gpt4 book ai didi

python - WebElement 上的 Selenium WebDriver “find_element_by_xpath”

转载 作者:行者123 更新时间:2023-12-01 06:44:13 24 4
gpt4 key购买 nike

<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>

我想通过find_element_xpath找到按钮,但它总是出现

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

任何帮助将不胜感激。

    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)

最佳答案

要单击按钮,会引发 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()

您需要导入以下库。

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

已编辑

要获取按钮的文本,请使用下面的代码。use 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"))

关于python - WebElement 上的 Selenium WebDriver “find_element_by_xpath”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59321463/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com