python - 找不到特定元素。硒。Python
问题描述
自动填写表格。找不到月份选项。
此代码选择第 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 个答案都是可行的,我掷硬币选择接受哪个答案以避免偏见。谢谢您的帮助
解决方案
There are 5 elements with
//option[contains(@value,'06')][1]
so use find_elements
first like this :
all_elements = driver.find_elements_by_xpath("//option[contains(@value,'06')][1]")
and then click on whichever you want :-
all_elements[0].click() #to click on first element.
Also, full code to select date, Month, year using JS would be :
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'")
if you notice we are using index, so it should select 2nd-December-2018
Update 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')
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Note that, Using JS is not recommended if the job can be done via Select class, Please see the updated 1 code.
推荐阅读
- reporting-services - SSRS IIF 表达式为 False 值返回 #Error 但不为 True
- bash - Curl 使用变量而不是手动编写的 URL 引发错误 (3)
- amazon-web-services - 许多 aws sso-admin 命令以 403 失败
- javascript - How to create a JS Date object from weekday and time string
- python - 将数据抓取到 xlsx 文件中
- javascript - 计算滚动以在自定义栏上按比例显示
- python - 如何将文本放在中心以便您可以清楚地看到它?以及如何去掉每个数字末尾的零?
- reactjs - 反应私有路由不呈现内容
- spring-boot - 带 Spring 的 ZeroMQ (spring-integration-zeromq)
- python - 使用正则表达式根据单引号拆分熊猫行中的列表