1

I'm trying to click an input button whose main identifiers are dynamically created. Therefore I'm trying to click it based on the span information after it.

<span id="DYNAMIC" class="a-button a-button-primary pmts-button-input apx-compact-continue-action pmts-portal-component pmts-portal-components-DYNAMIC primary-action-button">
<span class="a-button-inner">
    <input data-pmts-component-id="DYNAMIC" class="a-button-input a-button-text" type="submit" aria-labelledby="DYNAMIC">
        <span id="DYNAMIC" class="a-button-text" aria-hidden="true">
            <span>Use this payment method</span>
        </span>
</span>

I've put in the word DYNAMIC where the IDs are dynamically created rather than putting in the value. Below is what I think was my best version of the many things I tried but I've still not been able to accomplish the task.

var btnPaymentMethod = driver.FindElement(By.XPath("//input[normalize-space(.//span)='Use this payment method']"));
btnPaymentMethod.Click();
1
  • Seems your input contains 2 child spans What about driver.FindElement(By.XPath("//input[normalize-space(.//span//span)='Use this payment method']")); Commented Sep 17, 2019 at 14:30

4 Answers 4

3

To click on input button with reference to span tag.Use below xpath.

//span[text()='Use this payment method']/preceding::input[1]

Try the below code.

var btnPaymentMethod = driver.FindElement(By.XPath("//span[text()='Use this payment method']/preceding::input[1]"));
btnPaymentMethod.Click();
Sign up to request clarification or add additional context in comments.

Comments

0

Try this xpath,

var btnPaymentMethod = driver.FindElement(By.XPath("//*[contains(@span,'Use this payment method')]"));
btnPaymentMethod.Click();

Comments

0

To click() on the element with text as Use this payment method, you have to induce WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("span.a-button-inner span.a-button-text>span"))).Click();
    
  • xpath:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//span[@class='a-button-text']/span[text()='Use this payment method']"))).Click();
    

Comments

0

The following xpath's will also locate the input element for you:

"//span[text() = 'Use this payment method']/../parent::span/input"

"//span[contains(text(), 'Use this payment method')]/../parent::span/input"

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.