【问题标题】:Can't locate certain element. Selenium. Python找不到特定元素。硒。 Python
【发布时间】:2021-08-23 13:31:29
【问题描述】:

自动填写表格。找不到月份选项。

此代码选择第 6 天作为日期选项。

driver.find_element_by_xpath("//option[contains(@value,'06')][1]").click()

我认为这段代码会调用第 12 个月,但在年份部分调用 1912

driver.find_element_by_xpath("//option[contains(@value,'12')][1]").click()

链接是https://users.premierleague.com/a/profile/register/personal

所有 3 个答案都是可行的,我掷硬币选择接受哪个答案以避免偏见。感谢您的帮助

【问题讨论】:

  • 除了在答案中做类似的事情之外,在这种情况下,我建议首先通过 id 选择 find_element_by_id 然后通过 xpath 以确保您只查看特定元素。

标签: python selenium xpath


【解决方案1】:

有5个元素

//option[contains(@value,'06')][1]

所以首先像这样使用find_elements

all_elements = driver.find_elements_by_xpath("//option[contains(@value,'06')][1]")

然后点击你想要的:-

all_elements[0].click() #to click on first element. 

另外,使用 JS 选择日期、月份、年份的完整代码是:

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.get("https://users.premierleague.com/a/profile/register/personal")
driver.execute_script("return document.getElementById('ismjs-profile-dob-day').selectedIndex = '2'")
driver.execute_script("return document.getElementById('ismjs-profile-dob-month').selectedIndex = '12'")
driver.execute_script("return document.getElementById('ismjs-profile-dob-year').selectedIndex = '4'")

如果你注意到我们正在使用索引,那么它应该选择2nd-December-2018

更新 1:

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.get("https://users.premierleague.com/a/profile/register/personal")
wait = WebDriverWait(driver, 50)
day = Select(wait.until(EC.visibility_of_element_located((By.ID, "ismjs-profile-dob-day"))))
month = Select(wait.until(EC.visibility_of_element_located((By.ID, "ismjs-profile-dob-month"))))
year = Select(wait.until(EC.visibility_of_element_located((By.ID, "ismjs-profile-dob-year"))))
day.select_by_value('04')
month.select_by_value('12')
year.select_by_value('2020')

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

注意,如果可以通过 Select 类完成工作,不建议使用 JS,请参阅更新的 1 代码。

【讨论】:

  • Selenium 文档明确声明不要混合隐式和显式等待。 Selenium 贡献者多次表示不要使用隐式等待,而是使用WebDriverWait
  • @JeffC :是的,我明白,但这与这个问题无关。我已经编辑了我的答案以删除隐式等待。
【解决方案2】:

该页面上有多个下拉菜单,每个下拉菜单都包含多个选项。
要选择月份选项,您可以像这样使用 XPath:

//select[contains(@id,'month')]//option[contains(@value,'12')]

要选择日期,您可以类似地使用

//select[contains(@id,'day')]//option[contains(@value,'12')]

等等

【讨论】:

  • tysm 我不知道你可以在 xpath 定位器中有 2 个包含
  • Xpath 是一个非常强大和灵活的工具,它有很多定位元素的策略。您目前熟悉的更多内容。
【解决方案3】:

有很多方法可以完成此任务,但到目前为止,这是解决此问题的最可定制的方法。

定位日期:

driver.find_element_by_xpath("//select[@id='ismjs-profile-dob-day']/option[text()='6']").click()

查找月份:

driver.find_element_by_xpath("//select[@id='ismjs-profile-dob-month']/option[value()='December']").click()

查找年份:

driver.find_element_by_xpath("//select[@id='ismjs-profile-dob-year']/option[text()='2015']").click()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 2020-08-17
    • 1970-01-01
    相关资源
    最近更新 更多