簡體   English   中英

如何使用 xpath 查找元素?

[英]How to find an element using xpath?

我是編碼新手,我正在嘗試創建一個結帳機器人。 我正在使用 selenium 來幫助我做到這一點。 到目前為止,我的代碼一直有效,直到結帳我似乎無法讓我的機器人單擊結帳按鈕。

這是我檢查結帳按鈕時得到的。

   <a href="https://www.bestbuy.ca/identity/global/signin?redirectUrl=https%3A%2F%2Fwww.bestbuy.ca%2Fcheckout%2F%3Fqit%3D1%23%2Fen-ca%2Fshipping%2FON%2FM4W&lang=en-CA&contextId=checkout" class="button_E6SE9 primary_1oCqK continueToCheckout_3Dgpe regular_1jnnf" data-automation="continue-to-checkout"> == $0

我努力了

find_element_by_class_name("continueToCheckout_3Dgpe")
find_element_by_xpath( '//*[@class="continueToCheckout_3Dgpe"]' ).click()
findElement(By.cssSelector("a[href*='https://www.bestbuy.ca/identity/global/signin?redirectUrl=https%3A%2F%2Fwww.bestbuy.ca%2Fcheckout%2F%3Fqit%3D1%23%2Fen-ca%2Fshipping%2FON%2FM4W&lang=en-CA&contextId=checkout']")).click();
find_element_by_partial_link_text('https://www.bestbuy.ca/identity/global/signin?redirectUrl=https%3A%2F%2Fwww.bestbuy.ca%2Fcheckout%2F%3Fqit%3D1%23%2Fen-ca%2Fshipping%2FON%2FM4W&lang=en-CA&contextId=checkout')
find_element_by_xpath('//a[@href="https://www.bestbuy.ca/identity/global/signin?redirectUrl=https%3A%2F%2Fwww.bestbuy.ca%2Fcheckout%2F%3Fqit%3D1%23%2Fen-ca%2Fshipping%2FON%2FM4W&lang=en-CA&contextId=checkout"]')
find_element(By.xpath("//a[@href='https://www.bestbuy.ca/identity/global/signin?redirectUrl=https%3A%2F%2Fwww.bestbuy.ca%2Fcheckout%2F%3Fqit%3D1%23%2Fen-ca%2Fshipping%2FON%2FM4W&lang=en-CA&contextId=checkout']"))

似乎沒有任何工作並且沒有單擊結帳按鈕。 如果有任何關於如何解決這個問題的想法會有所幫助:謝謝:)

driver.find_element_by_xpath("//a[@data-automation='continue-to-checkout']").click()

將單擊帶有屬性的標簽繼續結帳。 現在,如果您切換到該頁面等,您可能需要等待。

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//a[@data-automation='continue-to-checkout']"))).click()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM