NoSuchElementException:消息:尝试通过 Selenium 和 Python 单击 VISA 按钮时无法找到元素
Posted
技术标签:
【中文标题】NoSuchElementException:消息:尝试通过 Selenium 和 Python 单击 VISA 按钮时无法找到元素【英文标题】:NoSuchElementException: Message: Unable to locate element while trying to click on the button VISA through Selenium and Python 【发布时间】:2019-07-25 00:06:18 【问题描述】:我无法单击此按钮在我的机器人上创建结帐。 我想点击图片获取另一个页面。
<label for="VISA" class="choiceLabel">
<input type="radio" class="visuallyhidden" name="cardTypeRadio" id="VISA" value="VISA" title="VISA" onclick="validateAndSubmit('VISA');">
<span class="imgElt xh-highlight" onclick="validateAndSubmit('VISA');">
<img src="/static/2.15.0.1/images/type-carte/visa.png" title="Visa">
</span>
<span class="txtElt">Visa</span>
</label>
这是我的代码:
try:
check = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID,"VISA" )))
print ("Page is ready!")
visa = driver.find_elements_by_xpath("label[@class='choiceLabel'][4]")
visa.click()
except TimeoutException:
print ("Loading took too much time!")
return check
我收到此错误:
File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "c:/Users/pietro/Documents/monitor/x/bot.py", line 48, in all
visa = driver.find_element_by_xpath("label[@class='choiceLabel'][4]")
File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value)['value']
File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: label[@class='choiceLabel'][4]
【问题讨论】:
看起来 xpath 不正确。你是如何提取 xpath 的? 【参考方案1】:在我的机器人上创建结帐的按钮似乎是一个信用卡相关字段,而历史上信用卡相关字段位于@ 987654323@.
您可以在以下位置找到一些相关讨论:
Unable to locate element of credit card number using selenium python org.openqa.selenium.NoSuchElementException: Returned node (null) was not a DOM element when trying to locate card-fields-iframe by CssSelector因此,如果所需的元素在 <iframe>
内,那么您必须:
您可以使用以下任一解决方案:
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe_css_selector")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='VISA']"))).click()
使用XPATH
:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe_xpath")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@for='VISA']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
【讨论】:
【参考方案2】:您正在搜索的 xPath 可能看起来更像这样:
//input[@name='cardTypeRadio' and @value='VISA']
如果这不能为您提供独特的元素,我们可以对其进行一些改进。
【讨论】:
【参考方案3】:可能你的xpath
是错误的。试试下面的代码。希望这会有所帮助。
driver.find_element_by_xpath("//input[@name='cardTypeRadio']").click()
或
driver.find_element_by_xpath("//input[@id='VISA']").click()
如果webdriver
点击不起作用。请尝试使用 javascript 进行点击。
driver.execute_script("arguments[0].click();", driver.find_element_by_xpath("//input[@name='cardTypeRadio']"))
driver.execute_script("arguments[0].click();", driver.find_element_by_xpath("//input[@id='VISA']"))
【讨论】:
以上是关于NoSuchElementException:消息:尝试通过 Selenium 和 Python 单击 VISA 按钮时无法找到元素的主要内容,如果未能解决你的问题,请参考以下文章
NoSuchElementException:消息:尝试通过 Selenium 和 Python 单击 VISA 按钮时无法找到元素
selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:尝试使用 selenium 单击下一步按钮时无法找到元素
java throw new NoSuchElementException build failed with arraylist
jclouds with OpenStack => java.util.NoSuchElementException: apiType compute not found in catalog