如何使用 Selenium 和 Python 从下拉列表中 select 一个值
How to select a value from DropDown list using Selenium and Python
我有以下下拉列表,我想 select 值 "Previous Year" 来自:
(我已经 select 手动编辑了值)
问题有两方面,我很难 "finding" 或用我的代码找到这个下拉列表,"id" 中的数字一页一页地改变,所以我不能仅仅通过 "id" 来定位它。我正在尝试使用以下 Python 3 代码:
select = driver.find_elements_by_xpath('//*[contains(@id, "dropdown-intervaltype")]')
select.click()
# then click the "Previous Year" text
我也试过了:
select = Select(driver.find_elements_by_xpath('//*[contains(@id, "dropdown-intervaltype")]'))
select.select_by_value("Previous Year")
现在这个网站是一个私人工作网站,所以我不能授予访问权限,但我希望有人能指出我还能尝试找到这个元素的其他方法和 select 正确的放置贬值?我已经尝试通过 "css" 和 "xpath" 定位该字段,但没有成功。
xpath = //*[@id="dropdown-intervaltype31442"]
css_selector = #dropdown-intervaltype31442
full_xpath = /html/body/div[4]/div[2]/div/div[3]/div[3]/div/div[2]/div/form/table/tbody/tr/td[2]/div/div[1]/select
任何要测试的想法都会有所帮助。谢谢!
<select>
元素是一个动态元素。因此 select 文本为 上一年 的 <option>
使用 you need to induce for the element_to_be_clickable()
and you can use either of the following :
使用CSS_SELECTOR
:
select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select[id^='dropdown-intervaltype'][name^='deviceServicesModel']"))))
select.select_by_visible_text('Previous Year')
使用XPATH
:
select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[starts-with(@id, 'dropdown-intervaltype')][starts-with(@name,'deviceServicesModel')]"))))
select.select_by_visible_text('Previous Year')
注意:您必须添加以下导入:
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
参考资料
您可以在以下位置找到一些相关讨论:
您可以使用 XPath:
new Select (driver.findElement(By.xpath("//select[starts-with(@id, 'dropdown-intervaltype')][starts- with(@name,'deviceServicesModel')]")).selectByVisibleText("Privious Year");
你也可以使用id:
新 Select(driver.findElement(By.id("dropdownintervaltype"))).selectByVisibleText("Argentina");
您检查您的 xpath 并采用正确的 xpath。希望这个回答对你有用。
我有以下下拉列表,我想 select 值 "Previous Year" 来自:
(我已经 select 手动编辑了值)
问题有两方面,我很难 "finding" 或用我的代码找到这个下拉列表,"id" 中的数字一页一页地改变,所以我不能仅仅通过 "id" 来定位它。我正在尝试使用以下 Python 3 代码:
select = driver.find_elements_by_xpath('//*[contains(@id, "dropdown-intervaltype")]')
select.click()
# then click the "Previous Year" text
我也试过了:
select = Select(driver.find_elements_by_xpath('//*[contains(@id, "dropdown-intervaltype")]'))
select.select_by_value("Previous Year")
现在这个网站是一个私人工作网站,所以我不能授予访问权限,但我希望有人能指出我还能尝试找到这个元素的其他方法和 select 正确的放置贬值?我已经尝试通过 "css" 和 "xpath" 定位该字段,但没有成功。
xpath = //*[@id="dropdown-intervaltype31442"]
css_selector = #dropdown-intervaltype31442
full_xpath = /html/body/div[4]/div[2]/div/div[3]/div[3]/div/div[2]/div/form/table/tbody/tr/td[2]/div/div[1]/select
任何要测试的想法都会有所帮助。谢谢!
<select>
元素是一个动态元素。因此 select 文本为 上一年 的 <option>
使用 element_to_be_clickable()
and you can use either of the following
使用
CSS_SELECTOR
:select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select[id^='dropdown-intervaltype'][name^='deviceServicesModel']")))) select.select_by_visible_text('Previous Year')
使用
XPATH
:select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[starts-with(@id, 'dropdown-intervaltype')][starts-with(@name,'deviceServicesModel')]")))) select.select_by_visible_text('Previous Year')
注意:您必须添加以下导入:
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
参考资料
您可以在以下位置找到一些相关讨论:
您可以使用 XPath:
new Select (driver.findElement(By.xpath("//select[starts-with(@id, 'dropdown-intervaltype')][starts- with(@name,'deviceServicesModel')]")).selectByVisibleText("Privious Year");
你也可以使用id:
新 Select(driver.findElement(By.id("dropdownintervaltype"))).selectByVisibleText("Argentina");
您检查您的 xpath 并采用正确的 xpath。希望这个回答对你有用。