錯誤訊息:
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()
uj5u.com熱心網友回復:
該元素Cancel沒有 innerText 而是該value屬性設定為Cancel
<input type="button" name="cancel" onclick="parent.location='/'" class="secondary button fiftyfifty mb16" value="Cancel">
解決方案
要單擊 element 而不是present_of_element_located 
uj5u.com熱心網友回復:
您嘗試訪問的元素是input元素 NOTbutton元素。
使用以下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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/465600.html
標籤:Python 硒 硒网络驱动程序 路径 网络驱动程序等待
上一篇:如何從<a>標簽中獲取內部文本
