問題描述
我無法單擊此按鈕在我的機器人上創建結帳.我想點擊圖片獲取另一個頁面.
I can't click on this button to create a checkout on my bot. I want to click the image to get another page.
<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" alt="VISA" 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:UsersxAppDataLocalProgramsPythonPython37lib hreading.py", line 917, in _bootstrap_inner
self.run()
File "C:UsersxAppDataLocalProgramsPythonPython37lib hreading.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:UsersxAppDataLocalProgramsPythonPython37libsite-packagesseleniumwebdriver
emotewebdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:UsersxAppDataLocalProgramsPythonPython37libsite-packagesseleniumwebdriver
emotewebdriver.py", line 978, in find_element
'value': value})['value']
File "C:UsersxAppDataLocalProgramsPythonPython37libsite-packagesseleniumwebdriver
emotewebdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:UsersxAppDataLocalProgramsPythonPython37libsite-packagesseleniumwebdriver
emoteerrorhandler.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]
推薦答案
在我的機器人上創建結帳的按鈕 似乎是一個 信用卡 相關領域,并且在歷史上信用卡相關字段位于<iframe>
中.
The button to create a checkout on my bot seems to be a Credit Card related field and historically Credit Card related fields resides within <iframe>
.
您可以在以下位置找到一些相關討論:
You can find a couple of relevant discussions in:
- 無法定位使用 selenium python 的信用卡號元素
- org.openqa.selenium.NoSuchElementException:嘗試通過 CssSelector 定位 card-fields-iframe 時返回的節點(null)不是 DOM 元素
因此,如果所需的元素在 <iframe>
內,那么您必須:
So if the the desired element is within an <iframe>
so you have to:
- 誘導 WebDriverWait 使所需的框架可用并切換到它.
- 誘導 WebDriverWait 使所需的元素可點擊.
您可以使用以下任一解決方案:
- Induce WebDriverWait for the desired frame to be available and switch to it.
- Induce WebDriverWait for the desired element to be clickable.
You can use either of the following solutions:
使用
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,"http://iframe_xpath")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "http://label[@for='VISA']"))).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
這篇關于NoSuchElementException:消息:嘗試通過 Selenium 和 Python 單擊 VISA 按鈕時無法找到元素的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!