gpt4 book ai didi

Python Selenium - 如何点击 javascript 按钮

转载 作者:太空宇宙 更新时间:2023-11-03 13:46:12 25 4
gpt4 key购买 nike

我是 Python 和 Selenium 的新手。我的网站上有一个按钮的以下代码,但我无法使用

单击它
 driver.find_element_by_id("AddToBasket").click()

driver.find_element_by_xpath("//a[@id='AddToCartButton']").click()

将鼠标悬停在按钮上只会显示 javascript:void(0)

这是我的代码

<div class="add">
<a href="javascript:void(0)" id="AddToBasket" class="addtobtn addtobag">
<span>Add to cart</span>
</a>

谢谢。

最佳答案

from selenium.webdriver.common.action_chains import ActionChains
self.driver = webdriver.Firefox()
# You need a mouse to hover the span elements here
self.mouse = webdriver.ActionChains(self.driver)

# You need get the span element from its xpath:
value = 'Add to cart'
span_xpath = '//span[contains(text(), "' + value + '")]'
span_element = driver.find_element_by_xpath(span_xpath)

# Then you hover on span element by mouse and click on it:
self.mouse.move_to_element(span_element).click().perform()

关于Python Selenium - 如何点击 javascript 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19897183/

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