简体   繁体   English

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

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

I want to create an automatic creation for Nike accounts.我想为 Nike 帐户创建一个自动创建。 For that I need to add a phone number.为此,我需要添加一个电话号码。 I am coding with Python 3, Selenium and the Chrome Webdriver.我正在使用 Python 3、Selenium 和 Chrome Webdriver 进行编码。 This is my current code:这是我当前的代码:

    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)

This codes only works sometimes, I am often getting this error message:此代码有时仅有效,我经常收到此错误消息:

      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: 

Do you maybe know a way to fix this?你可能知道解决这个问题的方法吗? If you want to inspect the page, I created an account for you with which you can go onto the site and inspect the site.如果您想检查该页面,我为您创建了一个帐户,您可以使用该帐户访问该站点并检查该站点。 Click to go to Site点击进入网站

Account credentials:账户凭证:

Mail: eXrWi9TfA5XSfNcu4uv2q1@peter.de


Password: 5By3oq1Bw

I want to click this button:我想点击这个按钮:

按钮

I wish I could tell you that this works 100% of the time (it doesn't) but perhaps it will get you a bit closer.我希望我能告诉你这 100% 的时间有效(它没有),但也许它会让你更接近一点。 I think one of the problems is that when you login the settings for Account are not open and visible (at least that is always the case for me) and you have to first click on the Account selection at the left to reveal the Mobil settings.我认为其中一个问题是,当您登录时,帐户的设置未打开且不可见(至少对我而言始终如此),您必须先单击左侧的帐户选择以显示美孚设置。 Unfortunately, even that does not work 100% of the time, at least not with the following code (I am using basic element click calls).不幸的是,即使这样也不能 100% 地工作,至少不能使用以下代码(我使用的是基本元素点击调用)。 But I offer this up for what it's worth:但我提供它的价值:

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()

To click on the element with text as Hinzufügen instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :要单击文本为Hinzufügen而不是presence_of_element_located()的元素,您需要为element_to_be_clickable()引入WebDriverWait ,您可以使用以下任一定位器策略

  • Using CSS_SELECTOR :使用CSS_SELECTOR

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

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

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

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

I was quite new to coding, I still am, and I just wanted to create a little script for myself.我对编码很陌生,我仍然是,我只是想为自己创建一个小脚本。 Also I was quite unexperienced with XPaths.此外,我对 XPaths 非常缺乏经验。 I recommend everybody who stumbles upon this issue, to read through this article .我建议每个偶然发现这个问题的人通读这篇文章

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 selenium.common.exceptions.TimeoutException 错误使用 WebDriverWait 通过 Selenium 和 Python - selenium.common.exceptions.TimeoutException error using WebDriverWait with expected_conditions through Selenium and Python 通过硒和python 3使用element_to_be_clickable方法时,Expected_Conditions返回TimeoutException - Expected_Conditions returns TimeoutException while using element_to_be_clickable method through selenium & python 3 python selenium present_of_element_located超时 - python selenium presence_of_element_located timeout selenium.common.exceptions.TimeoutException:当我尝试使用python进行按钮单击时,将出现此错误 - selenium.common.exceptions.TimeoutException: this error will getting when I'm trying to button click using python 引发TimeoutException(消息,屏幕,堆栈跟踪)selenium.common.exceptions.TimeoutException:消息:使用Selenium Python - raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: using Selenium Python python和Selenium不一致的selenium.common.exceptions.TimeoutException - Inconsistent selenium.common.exceptions.TimeoutException with Python and Selenium PYTHON:Selenium 循环 selenium.common.exceptions.TimeoutException - PYTHON : Selenium loop selenium.common.exceptions.TimeoutException python selenium:selenium.common.exceptions.TimeoutException:消息:第20行 - python selenium : selenium.common.exceptions.TimeoutException: Message: line 20 Selenium:Selenium中的summary_of_element_located()聚合 - Selenium: aggregate Selenium By in presence_of_element_located() selenium.common.exceptions.TimeoutException:使用 Selenium ZA7F1735426B93273 将文本发送到 iframe 中的用户名字段的消息错误 - selenium.common.exceptions.TimeoutException: Message error sending text to username field within iframe using Selenium Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM