Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium-如何单击javascript按钮_Python_Selenium - Fatal编程技术网

Python Selenium-如何单击javascript按钮

Python Selenium-如何单击javascript按钮,python,selenium,Python,Selenium,我不熟悉Python和Selenium。我的网站上有一个按钮的以下代码,我无法使用 driver.find_element_by_id("AddToBasket").click() 或 将鼠标悬停在按钮上只会显示javascript:void(0) 这是我的代码 <div class="add"> <a href="javascript:void(0)" id="AddToBasket" class="addtobtn addtobag"> <

我不熟悉Python和Selenium。我的网站上有一个按钮的以下代码,我无法使用

 driver.find_element_by_id("AddToBasket").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()
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()