【问题标题】:C# Selenium XPATH Click Input based on text from SpanC# Selenium XPATH 基于 Span 中的文本单击输入
【发布时间】:2020-01-18 10:22:06
【问题描述】:

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

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

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

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

【问题讨论】:

  • 似乎您的输入包含 2 个子跨度 那么 driver.FindElement(By.XPath("//input[normalize-space(.//span//span)='使用此付款方式'] "));

标签: c# selenium xpath css-selectors webdriverwait


【解决方案1】:

以下 xpath 还将为您定位输入元素:

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

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

【讨论】:

    【解决方案2】:

    click()元素上的文本为使用此付款方式,您必须为所需的ElementToBeClickable()诱导WebDriverWait,您可以使用任何一种关注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();
      

    【讨论】:

      【解决方案3】:

      参考 span 标签点击输入按钮。在 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();
      

      【讨论】:

        【解决方案4】:

        试试这个 xpath,

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-04-22
          • 2016-12-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-09
          • 2019-07-22
          • 1970-01-01
          相关资源
          最近更新 更多