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();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Roro
  • 311
  • 2
  • 20
  • Seems your input contains 2 child spans What about driver.FindElement(By.XPath("//input[normalize-space(.//span//span)='Use this payment method']")); – Somber Sep 17 '19 at 14:30

4 Answers4

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();
KunduK
  • 32,888
  • 5
  • 17
  • 41
0

Try this xpath,

var btnPaymentMethod = driver.FindElement(By.XPath("//*[contains(@span,'Use this payment method')]"));
btnPaymentMethod.Click();
Sureshmani Kalirajan
  • 1,938
  • 2
  • 9
  • 18
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();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
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"
SandstormNick
  • 1,821
  • 2
  • 13
  • 24