简体   繁体   English

WebElement 上的 Selenium WebDriver “find_element_by_xpath”

[英]Selenium WebDriver “find_element_by_xpath” on WebElement

 <figcaption class="ncss-row"> <div class="ncss-col-sm-12 full"> <div class="figcaption-content"> <div class="copy-container ta-sm-c bg-white pt6-sm pb7-sm pb5-lg"> <h3 class="ncss-brand u-uppercase text-color-grey mb-1-sm mb0-md mb-3-lg fs12-sm fs14-md"> Air Jordan XI<!-- -->&nbsp; </h3> <h6 class="ncss-brand u-uppercase fs20-sm fs24-md fs28-lg">Black/Red<!-- -->&nbsp; </h6> </div> <div data-qa="shop-now-cta" class="cta-container bg-white pt6-sm pb7-sm pb5-lg prl12-sm pb8-sm pt8-lg ta-sm-c xh-highlight"> <div> <button class="cta-btn u-uppercase ncss-btn text-color-white ncss-brand d-sm-b d-lg-ib pr5- sm pl5-sm pt3-sm pb3-sm d-sm-ib bg-black test-buyable buyable-full-width buyable-feed" data-qa="notify-me-cta theme-feed">通知我</button> </div> </div> </div> </div> </figcaption>

I want to locate the button by find_element_xpath, bu it always always appears我想通过 find_element_xpath 定位按钮,但它总是出现

[<selenium.webdriver.remote.webelement.WebElement (session="10bf8a8d83d1b36188a6b1e41856c052", element="0.5294218443885808-2")>]

Any help would be greatly appreciated.任何帮助将不胜感激。

    url = 'https://www.nike.com/cn/launch/'
browser = webdriver.Chrome()
browser.get(url)
elements = browser.find_element_by_xpath('//figure/div/div/figcaption/div/div/div[2]/div/button').text
print(elements)

To Click on the Button Induce WebDriverWait and wait for element_to_be_clickable () And use following Css selector.单击按钮 Induce WebDriverWait并等待element_to_be_clickable () 并使用以下 Css 选择器。

WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'div[data-qa="shop-now-cta"] button[data-qa="notify-me-cta theme-feed"]'))).click()

You need to import following libraries.您需要导入以下库。

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

Edited已编辑

To get the Text of the button use below code.use get_attribute("textContent") Or get_attribute("innerHTML") since the item text is hidden on the page.要获取按钮的文本,请使用下面的代码。使用get_attribute("textContent")get_attribute("innerHTML")因为项目文本隐藏在页面上。

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

browser=webdriver.Chrome()
browser.get("https://www.nike.com/cn/launch/")
print(WebDriverWait(browser,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,'div[data-qa="shop-now-cta"] button[data-qa="notify-me-cta theme-feed"]'))).get_attribute("textContent"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM