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

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

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

问题

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

day = 6
month = 'May'
Year = 1999

driver.find_elements_by_xpath("//*[@class='border rounded-0 form-control form-control-sm']").click()
driver.find_element_by_xpath("/option[text()='"+str(day)+"']").click()
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:

<span class="float-left d-sm-flex align-items-sm-center"
    style="width: 100%;font-size: 13px;line-height: 20px;color: #6b747d;height: 20px;">* Date of birth:</span>
<p></p>
    
<select class="border rounded-0 form-control form-control-sm" type="text" name="xrbzm" required=""
    style="font-size: 12px;height: 2%;margin-bottom: 2%;width: 25%!important;float:left;" id="xrbzm1">
    <option value="" disabled="" selected="">Day</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>

</select>

<select class="border rounded-0 form-control form-control-sm" type="text" name="tzkrv" required=""
    style="font-size: 12px;height: 2%;margin-bottom: 2%;width: 45%!important;float:left;" id="tzkrv1">
    <option value="" disabled="" selected="">Month</option>
    <option value="01">January</option>
    <option value="02">February</option>
    <option value="03">March</option>
    <option value="04">April</option>
    <option value="05">May</option>
    <option value="06">June</option>
    <option value="07">July</option>
    <option value="08">August</option>
    <option value="09">Septmeber</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">Dicember</option>
</select>

<select class="border rounded-0 form-control form-control-sm" type="text" name="eawfb" required=""
    style="font-size: 12px;height: 2%;margin-bottom: 2%;width: 30%!important;float:left;" id="eawfb1">
    <option value="" disabled="" selected="">Year</option>
    <option value="2005">2005</option>
    <option value="2004">2004</option>
    <option value="2003">2003</option>
    <option value="2002">2002</option>
    <option value="2001">2001</option>
    <option value="2000">2000</option>
    <option value="1999">1999</option>
    <option value="1998">1998</option>
</select>

</body>

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

day = 6
month = 'May'
Year = 1999

driver.find_elements_by_xpath("//*[@class='border rounded-0 form-control form-control-sm']").click()
driver.find_element_by_xpath("/option[text()='"+str(day)+"']").click()
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

只翻译代码部分,如下:

Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(., 'Date of birth')]//following::select[1]")))).select_by_index(6)
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')
Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(., 'Date of birth')]//following::select[3]")))).select_by_value('1999')

以及以下的注释部分:

# 注意:您需要添加以下导入语句:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
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:

Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(., 'Date of birth')]//following::select[1]")))).select_by_index(6)
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')
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 :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
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:

确定