如何使用Selenium选择具有动态ID和名称值的下拉选项。

huangapple go评论104阅读模式
英文:

How to select dropdown options with dynamic id and name values using Selenium

问题

以下是要翻译的代码部分:

  1. day = 6
  2. month = 'May'
  3. Year = 1999
  4. driver.find_elements_by_xpath("//*[@class='border rounded-0 form-control form-control-sm']").click()
  5. driver.find_element_by_xpath("/option[text()='"+str(day)+"']").click()
  6. driver.find_element_by_xpath("//input[@placeholder='day']").click()

很抱歉,上述代码中的几行不起作用。

我如何在不使用“ID”或“NAME”选择器的情况下点击值选项呢?

请注意,这段代码中有一些问题,可能导致它无法正常工作。如果您需要进一步的帮助,请提供更多关于您的目标和网页结构的信息,以便我能够为您提供更具体的建议。

英文:

I want to click a dropdown from a html webpage and click a chosen dropdown option.

Unfortunately I can't find the element because everytime I load the page the id and name values change with random letters and numbers. So I have to let the driver find using different value.

Here's the html code:

  1. <span class="float-left d-sm-flex align-items-sm-center"
  2. style="width: 100%;font-size: 13px;line-height: 20px;color: #6b747d;height: 20px;">* Date of birth:</span>
  3. <p></p>
  4. <select class="border rounded-0 form-control form-control-sm" type="text" name="xrbzm" required=""
  5. style="font-size: 12px;height: 2%;margin-bottom: 2%;width: 25%!important;float:left;" id="xrbzm1">
  6. <option value="" disabled="" selected="">Day</option>
  7. <option value="1">1</option>
  8. <option value="2">2</option>
  9. <option value="3">3</option>
  10. <option value="4">4</option>
  11. <option value="5">5</option>
  12. <option value="6">6</option>
  13. <option value="7">7</option>
  14. <option value="8">8</option>
  15. </select>
  16. <select class="border rounded-0 form-control form-control-sm" type="text" name="tzkrv" required=""
  17. style="font-size: 12px;height: 2%;margin-bottom: 2%;width: 45%!important;float:left;" id="tzkrv1">
  18. <option value="" disabled="" selected="">Month</option>
  19. <option value="01">January</option>
  20. <option value="02">February</option>
  21. <option value="03">March</option>
  22. <option value="04">April</option>
  23. <option value="05">May</option>
  24. <option value="06">June</option>
  25. <option value="07">July</option>
  26. <option value="08">August</option>
  27. <option value="09">Septmeber</option>
  28. <option value="10">October</option>
  29. <option value="11">November</option>
  30. <option value="12">Dicember</option>
  31. </select>
  32. <select class="border rounded-0 form-control form-control-sm" type="text" name="eawfb" required=""
  33. style="font-size: 12px;height: 2%;margin-bottom: 2%;width: 30%!important;float:left;" id="eawfb1">
  34. <option value="" disabled="" selected="">Year</option>
  35. <option value="2005">2005</option>
  36. <option value="2004">2004</option>
  37. <option value="2003">2003</option>
  38. <option value="2002">2002</option>
  39. <option value="2001">2001</option>
  40. <option value="2000">2000</option>
  41. <option value="1999">1999</option>
  42. <option value="1998">1998</option>
  43. </select>
  44. </body>

This is the code I used to click the dropdown options:

  1. day = 6
  2. month = 'May'
  3. Year = 1999
  4. driver.find_elements_by_xpath("//*[@class='border rounded-0 form-control form-control-sm']").click()
  5. driver.find_element_by_xpath("/option[text()='"+str(day)+"']").click()
  6. driver.find_element_by_xpath("//input[@placeholder='day']").click()

Unfortunately none of those lines work.

How can I click the value options without using ID or NAME Selector?

答案1

得分: 1

只翻译代码部分,如下:

  1. Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(., 'Date of birth')]//following::select[1]")))).select_by_index(6)
  2. Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(., 'Date of birth')]//following::select[2]")))).select_by_visible_text('May')
  3. Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(., 'Date of birth')]//following::select[3]")))).select_by_value('1999')

以及以下的注释部分:

  1. # 注意:您需要添加以下导入语句:
  2. from selenium.webdriver.support.ui import WebDriverWait
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.support import expected_conditions as EC
  5. from selenium.webdriver.support.ui import Select
英文:

To select the <option>s for day = 6, month = 'May' and Year = 1999 you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  1. Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(., 'Date of birth')]//following::select[1]")))).select_by_index(6)
  2. Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(., 'Date of birth')]//following::select[2]")))).select_by_visible_text('May')
  3. Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(., 'Date of birth')]//following::select[3]")))).select_by_value('1999')

Note : You have to add the following imports :

  1. from selenium.webdriver.support.ui import WebDriverWait
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.support import expected_conditions as EC
  4. from selenium.webdriver.support.ui import Select

huangapple
  • 本文由 发表于 2023年2月14日 06:33:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定