selenium.common.exceptions.TimeoutException 在 Selenium Python 中使用 expected_conditions presence_of_element_located 单击按钮时

selenium.common.exceptions.TimeoutException while clicking on a button using expected_conditions presence_of_element_located in Selenium Python

我想为 Nike 帐户创建自动创建。为此,我需要添加一个 phone 号码。我正在使用 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

我想点击这个按钮:

我希望我可以告诉您,这在 100% 的时间都有效(但不是),但也许它会让您更接近一点。我认为其中一个问题是,当您登录时 Account 的设置未打开且不可见(至少对我来说总是这样),您必须先单击 Account 选择左侧以显示 Mobil 设置。不幸的是,即使那样也不是 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()

要单击文本为 Hinzufügen 而不是 presence_of_element_located() 的元素,您需要诱导 for the element_to_be_clickable() and you can use either of the following :

  • 使用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
    

我对编码很陌生,现在仍然是,我只是想为自己创建一个小脚本。此外,我对 XPaths 还很陌生。我建议每个偶然发现这个问题的人通读 this article

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