繁体   English   中英

C#Selenium XPATH单击基于跨度文本的输入

[英]C# Selenium XPATH Click Input based on text from Span

我试图单击一个输入按钮,其主要标识符是动态创建的。 因此,我试图根据其后的跨度信息单击它。

<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>

我在单词DYNAMIC中输入了动态创建的ID,而不是输入值。 以下是我认为是我尝试过的许多事情的最好版本,但是我仍然无法完成任务。

var btnPaymentMethod = driver.FindElement(By.XPath("//input[normalize-space(.//span)='Use this payment method']"));
btnPaymentMethod.Click();

要使用跨度标签单击输入按钮,请在xpath下使用。

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

试试下面的代码。

var btnPaymentMethod = driver.FindElement(By.XPath("//span[text()='Use this payment method']/preceding::input[1]"));
btnPaymentMethod.Click();

试试这个xpath,

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

要使用文本作为使用此付款方式的元素上的click() ,您必须为所需的ElementToBeClickable()诱导WebDriverWait ,并且可以使用以下定位策略之一

  • 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(); 

以下xpath还将为您找到input元素:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM