3

I am new to Python and Selenium. I have the following code for a button on my site and I am unable to click on it using

 driver.find_element_by_id("AddToBasket").click()

or

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

Hovering over the button just displays javascript:void(0)

So here is my Code

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

Thanks.

1 Answer 1

6
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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.