[英]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:使用XPATH
和aria-label属性:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Mobilnummer hinzufügen']"))).click()
Using XPATH
and innerText attribute:使用XPATH
和innerText属性:
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.