0

I have button details like below.

<button type="button" class="add-to-cart button primary-button false item-action-button">
          Add to Cart
        </button>

And xpath is /html/body/div/div[2]/div[2]/div/div[4]/div/div[1]/button

How do I click on the button?

I tried using this find_element_by_xpath('/button[contains(text(), "Add to Cart")]')

But it doesn't work. I don't want to just do .click() on given xpath. Is there way to click by finding text?

I am automating using python

2 Answers 2

1

/ Selects from the root node

// Selects nodes in the document from the current node that match the selection doesn't matter where they are

find_element_by_xpath('/button[contains(text(), "Add to Cart")]')

Since you are selecting from current node use

find_element_by_xpath('//button[contains(text(), "Add to Cart")]')

Hope this helps you...Kindly get back if there are any issues

Sign up to request clarification or add additional context in comments.

Comments

0

Try this one

link = driver.find_element_by_link_text(' Add to Cart')
link.click()

if still it does not works then go to the source of page you will see a Iframe you need to first switch into it

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.