1

I want to achieve clicking a HTML button via reading its Value or innerText in C# Selenium, none of the methods for click is seem to work, no idea why?

I was wondering if this is possible if so please advise my code is below.

Html below

<div class="a-button-stack">
<span class="a-declarative" data-action="dp-pre-atc-declarative" data-dp-pre-atc-declarative="{}" id="atc-declarative">
<span id="submit.add-to-cart-ubb" class="a-button a-spacing-small a-button-primary a-button-icon">
<span class="a-button-inner">
<i class="a-icon a-icon-cart"></i><input id="add-to-cart-button-ubb" name="submit.add-to-cart-ubb" title="Add to Shopping Basket" data-hover="Select <b>__dims__</b> from the left<br> to add to Basket" class="a-button-input" type="submit" 
value="Add to Basket" aria-labelledby="submit.add-to-cart-ubb-announce">
<span id="submit.add-to-cart-ubb-announce" class="a-button-text" aria-hidden="true">Add to Basket</span></span></span>
</span>

</div>

My code in C# is the following:

driver.FindElement(By.XPath("//button[contains(text(), 'Add to Basket')]")).Click();

driver.FindElement(By.XPath("//button[contains(text(), 'Add to Basket')]")).Click();

driver.FindElement(By.XPath("//*[contains(text(),'Add to Basket')]")).Click();

driver.FindElement(By.XPath("//button[contains(text(), 'Add to Basket')]")).Click();

2 Answers 2

1

//input[@value='Add to Basket']

There is no button tag but you can grab the input tag by it's data attribute value like so.

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

Comments

0

You are trying to use an element of a button but a button element does not exist.

The add to cart button in the html is:

//input[@id='add-to-cart-button-ubb']

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.