C# Selenium XPATH 根据 Span 中的文本单击输入
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>
我在动态创建 ID 而不是输入值的地方输入了 DYNAMIC 一词。以下是我认为我尝试过的许多事情中最好的版本,但我仍然无法完成任务。
var btnPaymentMethod = driver.FindElement(By.XPath("//input[normalize-space(.//span)='Use this payment method']"));
btnPaymentMethod.Click();
试试这个 xpath,
var btnPaymentMethod = driver.FindElement(By.XPath("//*[contains(@span,'Use this payment method')]"));
btnPaymentMethod.Click();
参考 xpath 下面的 span tag.Use 点击输入按钮。
//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();
在文本为的元素上click()
使用此支付方式,你必须为所需的WebDriverWait ElementToBeClickable()
并且您可以使用以下任一项 :
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 也将为您定位输入元素:
"//span[text() = 'Use this payment method']/../parent::span/input"
"//span[contains(text(), 'Use this payment method')]/../parent::span/input"
我正在尝试单击其主要标识符是动态创建的输入按钮。因此,我试图根据它之后的跨度信息来单击它。
<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 而不是输入值的地方输入了 DYNAMIC 一词。以下是我认为我尝试过的许多事情中最好的版本,但我仍然无法完成任务。
var btnPaymentMethod = driver.FindElement(By.XPath("//input[normalize-space(.//span)='Use this payment method']"));
btnPaymentMethod.Click();
试试这个 xpath,
var btnPaymentMethod = driver.FindElement(By.XPath("//*[contains(@span,'Use this payment method')]"));
btnPaymentMethod.Click();
参考 xpath 下面的 span tag.Use 点击输入按钮。
//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();
在文本为的元素上click()
使用此支付方式,你必须为所需的WebDriverWait ElementToBeClickable()
并且您可以使用以下任一项
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 也将为您定位输入元素:
"//span[text() = 'Use this payment method']/../parent::span/input"
"//span[contains(text(), 'Use this payment method')]/../parent::span/input"