[英]Selenium is unable to find a specific button to click
我在 Python 上使用 Selenium 來自動化我在網站上的結帳過程。 問題是我無法點擊“Continue To Order Review”按鈕並嘗試了多種方法。 我不斷收到的錯誤是"Message: stale element reference: element is not attach to the page document" 。
我嘗試使用顯式等待但它不起作用:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//button[@data-attr='continueToOrderReviewBtn']"))).click()
我還嘗試找到包含文本的按鈕:
try:
element = driver.find_element_by_xpath("//button[contains(text(),'Continue To Order Review')]")
except NoSuchElementException:
print("Button not found")
action = ActionChains(driver)
action.move_to_element(element).click().perform()
<div class="ncss-col-sm-12 pb5-sm prl5-sm va-sm-t ta-sm-r">
<button data-attr="continueToOrderReviewBtn" class="ncss-brand pt2-sm pr5-sm pb2-sm pl5-sm ncss-btn-accent continueOrderReviewBtn mod-button-width ncss-brand
pt3-sm prl5-sm pb3-sm pt2-lg pb2-lg d-sm-b d-md-ib u-uppercase u-rounded fs14-sm">Continue To Order Review</button>
</div>
html 未封裝在 iFrame 中。 我還嘗試搜索相應的按鈕 xpath 但腳本似乎仍然無法找到並單擊它。 我仍然是使用 Selenium 並每天學習一些新東西的新手,但似乎我無法解決這個問題。
由於 DOM 操作,某些元素可能暫時無法訪問或被多次加載。 它會導致您收到的過時元素引用。
此問題的解決方案是多次加載元素:
for i in range(0, 10):
try:
element = driver.find_element_by_xpath("//button[contains(text(),'Continue To Order Review')]")
except NoSuchElementException:
print("Button not found")
from selenium.webdriver.common.keys import Keys
driver.get('https://www.examplesitenike.com')
# dret = ngRet({},ddriver,doptions)
element=driver.find_element(By.XPATH, '//*[@id="gen-nav-commerce-header-v2"]/div[3]/div[1]/div/div/div[3]/div/button')
modalwindow=driver.find_element(By.XPATH, '//*[@class="pre-modal-view"]')
if not modalwindow.is_displayed():
try:element.click()
except:pass
if not modalwindow.is_displayed():
try:driver.execute_script("arguments[0].click();", element)
except:pass
if not modalwindow.is_displayed():
element.sendKeys(Keys.RETURN)
inputemail=driver.find_element(By.XPATH, '//input[@type="email"]')
if inputemail.is_displayed():
inputemail.send_keys('mylogin')
inputemail=driver.find_element(By.XPATH, '//input[@type="password"]')
if inputemail.is_displayed():
inputemail.send_keys('mypassword')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.