无法使用 Xpath 访问 Selenium Python 中的按钮
Unable to access the button in Selenium Python using Xpath
错误信息:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[text()="Cancel"]"}
我也试过使用标记名作为按钮。
这个HTML为:
<form name="forgotPassword" id="forgotPassForm" action="https://login.salesforce.com/secur/forgotpassword.jsp"
autocomplete="off" method="post" target="_top" novalidate="novalidate">
<p class="username">To reset your password, enter your Salesforce username.</p>
<input type="hidden" name="locale" value="in" />
<div class="verifyform">
<label for="un" class="label">Username</label>
<input id="un" type="email" class="input wide mb12 mt8 username" onKeyPress="checkCaps(event)"
name="un" value="" autofocus />
<input type="submit" name="continue" id="continue" class="button primary fiftyfifty right focus" value="Continue" />
<input type="button" name="cancel" onclick="parent.location='/?locale=in'" class="secondary button fiftyfifty mb16" value="Cancel" >
</div>
<input type="hidden" name="lqs" value="locale=in" />
<div id='pwcaps' class='pwcaps' style='display:none'>Caps Lock is on. Please try to log in again and remember that passwords are case-sensitive.</div>
<a class="hiddenlink" href="javascript:document.forgotPassword.submit();" id="continueReset">Continue</a>
<p class="small">Video: <a href="http://salesforce.vidyard.com/watch/MxeeKTO3x5oMx4jNVWWX4w" id="video-link">Need Help Logging In?</a></p>
</form>
我的代码:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(service=Service("C:\Users\Dell\Desktop\Selenium\chromedriver.exe"))
driver.maximize_window()
driver.get("https://login.salesforce.com/")
driver.find_element(By.CSS_SELECTOR, '#username').send_keys('Tac')
driver.find_element(By.CSS_SELECTOR, '.password').send_keys('PASSWRd')
driver.find_element(By.CSS_SELECTOR, '.password').clear()
driver.find_element(By.LINK_TEXT, 'Forgot Your Password?').click()
driver.find_element(By.XPATH, '//button[text()="Cancel"]').click()
你试过下面的代码吗:
driver.find_element(By.XPATH, '//*[@id="forgotPassForm"]/div[1]/input[3]').click()
要获得基本的 XPath,您可以通过右键单击网页并选择 'Elements' 来检查元素。然后您可以右键单击该按钮,然后单击 'Copy > Copy XPath'。这将使您能够在重构和使用更强大的 xcode.
之前获得非常基础的工作
见截图:
您尝试访问的元素是 input
元素而不是 button
元素。
使用以下 xpath
来识别元素。
driver.find_element(By.XPATH, "//input[@name='cancel' and @value='Cancel']").click()
要处理动态元素,请使用 WebDriverWait()
并等待元素可点击。
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='cancel' and @value='Cancel']"))).click()
您需要导入以下库。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
元素 Cancel 没有 innerText 而 value
属性设置为 取消
<input type="button" name="cancel" onclick="parent.location='/'" class="secondary button fiftyfifty mb16" value="Cancel">
解决方案
要单击元素而不是 you need to induce WebDriverWait for the and you can use either of the following :
使用CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[value='Cancel']"))).click()
使用 XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@value='Cancel']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
错误信息:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[text()="Cancel"]"}
我也试过使用标记名作为按钮。
这个HTML为:
<form name="forgotPassword" id="forgotPassForm" action="https://login.salesforce.com/secur/forgotpassword.jsp"
autocomplete="off" method="post" target="_top" novalidate="novalidate">
<p class="username">To reset your password, enter your Salesforce username.</p>
<input type="hidden" name="locale" value="in" />
<div class="verifyform">
<label for="un" class="label">Username</label>
<input id="un" type="email" class="input wide mb12 mt8 username" onKeyPress="checkCaps(event)"
name="un" value="" autofocus />
<input type="submit" name="continue" id="continue" class="button primary fiftyfifty right focus" value="Continue" />
<input type="button" name="cancel" onclick="parent.location='/?locale=in'" class="secondary button fiftyfifty mb16" value="Cancel" >
</div>
<input type="hidden" name="lqs" value="locale=in" />
<div id='pwcaps' class='pwcaps' style='display:none'>Caps Lock is on. Please try to log in again and remember that passwords are case-sensitive.</div>
<a class="hiddenlink" href="javascript:document.forgotPassword.submit();" id="continueReset">Continue</a>
<p class="small">Video: <a href="http://salesforce.vidyard.com/watch/MxeeKTO3x5oMx4jNVWWX4w" id="video-link">Need Help Logging In?</a></p>
</form>
我的代码:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(service=Service("C:\Users\Dell\Desktop\Selenium\chromedriver.exe"))
driver.maximize_window()
driver.get("https://login.salesforce.com/")
driver.find_element(By.CSS_SELECTOR, '#username').send_keys('Tac')
driver.find_element(By.CSS_SELECTOR, '.password').send_keys('PASSWRd')
driver.find_element(By.CSS_SELECTOR, '.password').clear()
driver.find_element(By.LINK_TEXT, 'Forgot Your Password?').click()
driver.find_element(By.XPATH, '//button[text()="Cancel"]').click()
你试过下面的代码吗:
driver.find_element(By.XPATH, '//*[@id="forgotPassForm"]/div[1]/input[3]').click()
要获得基本的 XPath,您可以通过右键单击网页并选择 'Elements' 来检查元素。然后您可以右键单击该按钮,然后单击 'Copy > Copy XPath'。这将使您能够在重构和使用更强大的 xcode.
之前获得非常基础的工作见截图:
您尝试访问的元素是 input
元素而不是 button
元素。
使用以下 xpath
来识别元素。
driver.find_element(By.XPATH, "//input[@name='cancel' and @value='Cancel']").click()
要处理动态元素,请使用 WebDriverWait()
并等待元素可点击。
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='cancel' and @value='Cancel']"))).click()
您需要导入以下库。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
元素 Cancel 没有 innerText 而 value
属性设置为 取消
<input type="button" name="cancel" onclick="parent.location='/'" class="secondary button fiftyfifty mb16" value="Cancel">
解决方案
要单击元素而不是
使用CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[value='Cancel']"))).click()
使用 XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@value='Cancel']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC