[英]I am trying to use selenium to load a waiting list and click the button, but I can't seem to find the element
I've tried to use find_element_by_class_name and link text and both result in NoSuchElement Exception.我尝试使用 find_element_by_class_name 和链接文本,都导致 NoSuchElement 异常。 I'm just trying to click this Join waitlist button for https://v2.waitwhile.com/l/fostersbarbershop/list-view - Any assistance is greatly appreciated.
我只是想点击https://v2.waitwhile.com/l/fostersbarbershop/list-view的加入候补名单按钮 - 非常感谢任何帮助。
from selenium import webdriver
import time
PATH = "C:\Python\Pycharm\attempt\Drivers\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://v2.waitwhile.com/l/fostersbarbershop/list-view")
join = False
while not join:
try:
joinButton = driver.find_element_by_class_name("disabled")
print("Button isnt ready yet.")
time.sleep(2)
driver.refresh()
except:
joinButton = driver.find_element_by_class_name("public-submit-btn")
print("Join")
joinButton.click()
join = True
The page your trying to automate is angular.您尝试自动化的页面是 angular。 Typically what happens in script-based pages is you download the source, the page load event is classed as complete, then some JS scripts run to get/update the page content.
通常在基于脚本的页面中发生的情况是您下载源代码,页面加载事件被归类为完成,然后运行一些 JS 脚本来获取/更新页面内容。 Those scripts (which can take seconds to complete) update the DOM with the page you see.
这些脚本(可能需要几秒钟才能完成)使用您看到的页面更新 DOM。
In contrast selenium can only recognise the page is loaded - Selenium will be attempting to find those elements at this readystate-point and is unaware of any running scripts.相比之下,selenium 只能识别页面已加载 - Selenium 将尝试在此就绪状态点找到这些元素,并且不知道任何正在运行的脚本。
You will need to wait for your element to be ready before you proceed.在继续之前,您需要等待元素准备好。
Easy solution is to add an implicit wait to your script.简单的解决方案是在脚本中添加隐式等待。 An implicit wait will ignore the nosuchelement exception until the timeout has been reached.
隐式等待将忽略 nosuchelement 异常,直到达到超时。
See here for more information on waits.有关等待的更多信息,请参见此处。
This is the line of code to add:这是要添加的代码行:
driver.implicitly_wait(10)
Just adjust the time based on what you need.只需根据您的需要调整时间。
You only need it once.你只需要一次。
This is the code that works for me:这是对我有用的代码:
driver = webdriver.Chrome()
driver.get("https://v2.waitwhile.com/l/fostersbarbershop/list-view")
driver.implicitly_wait(10)
join = False
while not join:
try:
joinButton = driver.find_element_by_class_name("disabled")
print("Button isnt ready yet.")
time.sleep(2)
driver.refresh()
except:
joinButton = driver.find_element_by_class_name("public-submit-btn")
print("Join")
joinButton.click()
join = True
It seems you have synchronization issue.看来您有同步问题。
Induce WebDriverWait()
and wait for element_to_be_clickable()
for following ID locator诱导
WebDriverWait()
并等待element_to_be_clickable()
用于跟随 ID 定位器
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "join-waitlist"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "ww-name"))).send_keys("TestUser")
You need to import below libraries.您需要导入以下库。
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser snapshot浏览器快照
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.