[英]Python Selenium - How to click on a javascript button
我是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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.