【问题标题】:Unable to locate form submit button找不到表单提交按钮
【发布时间】:2019-07-18 09:56:38
【问题描述】:
option = ChromeOptions()
chrome_prefs = {}
driver = Chrome(chrome_options=option) #getting the web driver object
try:
    url="https://www.groupon.fr/merchant/center/"
    driver.get(url)
    driver.maximize_window()
    email="xyz"
    password="abc"
    email_box=WebDriverWait(driver,10).until(lambda x: x.find_element_by_id("emailInput"))
    email_box.send_keys(email)
    password_box=WebDriverWait(driver,10).until(lambda x: x.find_element_by_id("passwordInput"))
    password_box.send_keys(password)
    attempts=0
    while True:
        try:
            submit = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CLASS_NAME,"submitButton button-primary button-cta")))
            driver.execute_script("arguments[0].click();", submit)
            break
        except:
            traceback.print_exc(file=sys.stdout)
            attempts+=1
            if (attempts>3):
                raise Exception("Error")
            continue
finally:
    driver.quit()

输出

Traceback(最近一次调用最后一次):文件 “”,第 22 行,在提交 = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CLASS_NAME,"submitButton button-primary button-cta"))) 文件 "C:\Users\sheik\Anaconda2\lib\site-packages\selenium\webdriver\support\wait.py", 第 80 行,直到引发 TimeoutException(消息、屏幕、堆栈跟踪) 超时异常:消息:

我实际上是在尝试登录此页面,在填写了电子邮件和密码后,虽然我使用了正确的按钮类名,但我无法找到提交按钮。我也从 xpath 尝试过,但没有成功。

我的代码有什么问题?The html of the page

【问题讨论】:

  • 它应该做什么?您几乎没有提供任何信息。每个人都有比试图弄清楚你的代码应该做什么更好的事情要做。请编辑您的问题以提供背景和上下文。
  • 如果你也分享部分的html代码就好了。
  • 我已更新查询。附上页面的html图像。自从我第一次在这里发帖以来,请多多包涵,我正在为此苦苦挣扎。
  • 问题是你正在使用By.CLASS_NAME,但是发送了3个类名,“submitButton button-primary button-cta”。要么选择三个中的一个,要么将其转换为 CSS 选择器并使用所有三个,例如贾斯汀的回答。

标签: python selenium automation


【解决方案1】:

我的新机器上尚未安装 selenium,但您可以使用 CSS selectors 进行搜索。 我知道示例代码不是python,但您应该能够找出相应的python代码。

编辑。请尝试以下操作:

submit = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,".submitButton.button-primary.button-cta")))

【讨论】:

  • 好的,这解决了我的问题。虽然我不知道怎么做。但是很好,非常感谢。
  • 据我所知,By.CLASS_NAME 不适用于匹配多个类”。CSS 选择器允许您匹配多个类。
  • 唯一但按钮有三个类,而不是一个。当您同时提供所有三个时,By.CLASS_NAME 将不起作用。然而,CSS 选择器允许您搜索具有三个类的元素。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-23
  • 1970-01-01
  • 1970-01-01
  • 2013-04-10
  • 2015-11-19
  • 2016-08-16
  • 2018-06-30
相关资源
最近更新 更多