【问题标题】:selenium.common.exceptions.TimeoutException while clicking on a button using expected_conditions presence_of_element_located in Selenium Pythonselenium.common.exceptions.TimeoutException 在使用 Selenium Python 中的 expected_conditions presence_of_element_located 单击按钮时
【发布时间】:2020-09-07 20:47:08
【问题描述】:

我想为 Nike 帐户创建一个自动创建。为此,我需要添加一个电话号码。我正在使用 Python 3、Selenium 和 Chrome Webdriver 进行编码。这是我当前的代码:

    driver.get('https://www.nike.com/de/member/settings')
    element2 = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[3]/div/div[6]/div[2]/div[2]/div/form/div[2]/div[5]/div/div/div/div[2]/button")))
    driver.execute_script("arguments[0].click();", element2)
    time.sleep(1)

此代码有时有效,我经常收到此错误消息:

      Traceback (most recent call last):
      File "C:/Users/Marten/PycharmProjects/NikeSNKRS/main.py", line 239, in <module>
        element2 = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[3]/div/div[6]/div[2]/div[2]/div/form/div[2]/div[5]/div/div/div/div[2]/button")))
      File "C:\Users\Marten\PycharmProjects\NikeSNKRS\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
        raise TimeoutException(message, screen, stacktrace)
    selenium.common.exceptions.TimeoutException: Message: 

您可能知道解决此问题的方法吗? 如果您想检查该页面,我为您创建了一个帐户,您可以使用该帐户进入该站点并检查该站点。 Click to go to Site

帐户凭据:

Mail: eXrWi9TfA5XSfNcu4uv2q1@peter.de


Password: 5By3oq1Bw

我想点击这个按钮:

【问题讨论】:

  • 不要透露您的用户名和密码
  • 它只是一个“假”帐户。

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

我希望我能告诉你,这在 100% 的情况下都有效(它没有),但也许它会让你更接近一点。我认为其中一个问题是,当您登录时,Account 的设置不打开且不可见(至少对我来说总是如此),您必须先点击 Account 选择左侧显示美孚设置。不幸的是,即使这样也不能 100% 起作用,至少不能使用以下代码(我使用的是基本元素点击调用)。但我提供了它的价值:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(options=options)

try:
    driver.implicitly_wait(10) # wait up to 10 seconds before calls to find elements time out
    driver.get('https://www.nike.com/member/settings')
    input('pausing for login ...') # to allow manual login
    driver.get('https://www.nike.com/member/settings')
    elem = driver.find_element_by_xpath('/html/body/div[3]/div/div[6]/div[1]/div[1]/div') # Account settings
    elem.click()
    elem = driver.find_element_by_xpath('/html/body/div[3]/div/div[6]/div[2]/div[2]/div/form/div[2]/div[5]/div/div/div/div[2]/button')
    elem.click()
finally:
    input('pausing for inspection') # to allow inspection
    driver.quit()

【讨论】:

  • 谢谢,我会试试的! :) 我会在星期四给你答复。
【解决方案2】:

要点击文本为 Hinzufügen 而不是 presence_of_element_located() 的元素,您需要为 WebDriverWait 诱导 element_to_be_clickable(),您可以使用以下任一 Locator Strategies

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Mobilnummer hinzufügen']"))).click()
    
  • 使用XPATHaria-label 属性:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Mobilnummer hinzufügen']"))).click()
    
  • 使用XPATHinnerText 属性:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'Hinzufügen')]"))).click()
    
  • 注意:您必须添加以下导入:

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

【讨论】:

  • 谢谢,但我正在使用来自不同国家的代理。而且我认为您的解决方案仅适用于德语代理,或者当语言设置为德语时。
  • @MatrixSpielt 我没有考虑语言区域设置的情况。但是您所说的文本,即 Hinzufügen 应该由定位器策略定位,没有任何失败。
  • 好的,我试过你的方法,但我现在经常收到这个错误代码:Traceback(最近一次调用最后一次):文件“C:/Users/Marten/PycharmProjects/NikeSNKRS/testdelete.py”,第 221 行,在 WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Mobilnummer hinzufügen']"))).click() 文件 "C:\ Users\Marten\PycharmProjects\NikeSNKRS\venv\lib\site-packages\selenium\webdriver\support\wait.py",第 80 行,直到引发 TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message :
【解决方案3】:

我对编码很陌生,我仍然是,我只是想为自己创建一个小脚本。此外,我对 XPaths 非常缺乏经验。我建议所有偶然发现这个问题的人,阅读this article

我使用了这个 XPath,而不是: //div[@class="mex-mobile-input"]/div/div/div[2]/button

【讨论】:

    猜你喜欢
    • 2020-11-26
    • 1970-01-01
    • 2021-01-31
    • 2020-08-06
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    相关资源
    最近更新 更多