简体   繁体   English

我正在尝试使用 selenium 加载等待列表并单击按钮,但我似乎找不到该元素

[英]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

This is the end state:这是state的结尾: 在此处输入图像描述

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.

相关问题 我找不到要单击的按钮元素 - I can't find the button element to click on it Selenium似乎找不到并单击网站上的按钮? -Python - Selenium can't seem to find and click a button on website? - Python 我正在尝试编写一个“机器人”服务员程序,但我似乎找不到我出错的地方 - I am trying to program a "robot" waiter program but I can't seem to find where I went wrong 我正在尝试使用 selenium python 在 chrome 新选项卡中单击一个元素,但我收到错误 no such element 即使该元素存在 - I am trying to use selenium python to click an element in chrome new tab, but I am getting error no such element even thought the element is there 我似乎无法在 Selenium 中单击此元素,有人知道为什么吗? - I can't seem to click this element in Selenium, does anyone know why? 如何使用Selenium单击无法检查的按钮? - How do I use Selenium to click on a button that can't be inspected? Selenium webdriverwait (.text_to_be_present _in_element doesn't) 似乎工作,我找不到任何解决方案 - Selenium webdriverwait (.text_to_be_present _in_element doesnt) seem to work and I can't find any solution to this 似乎在 python 中找不到具有 selenium 的元素 - Can't seem to find element with selenium in python 我正在尝试使用 Selenium 单击嵌套 div 中的按钮 - I am trying to click a button that is inside nested divs using Selenium 尝试使用 selenium 到 webscrape ncbi,数据不会加载并且不包含在具有我可以等待的 ID 的元素中 - Trying to use selenium to webscrape ncbi, the data doesn't load and isn't contained in an element with an ID that I can wait for
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM