【问题标题】:Selenium unable to find input text field within fieldsetSelenium 无法在字段集中找到输入文本字段
【发布时间】:2019-08-22 06:02:37
【问题描述】:

我在 selenium 查找输入文本字段时遇到问题 - 'billing-address__line-1 我使用的代码是 -

driver.findElement(By.xpath(".//fieldset[.//input[@id='billing-address__line-1']]"))

HTML 源代码

<body>
<div id="app-root">
<div class="">
<div class="c-navigation" role="banner">
<section class="app-checkout">
<section class="tickets-summary">
<div class="page-content page-content--mobile-full-width">
<div class="checkout-form">
<div class="app-checkout-form-feedback"/>
<form method="post" accept-charset="utf-8" id="checkout-form-passengers">
<form method="post" accept-charset="utf-8" id="checkout-form-contact-details">
<form method="post" accept-charset="utf-8" id="checkout-form-payment">
<div class="checkout-form-panel checkout-form-panel--no-padding app-payment" id="checkout-form-fieldset-payment">
<fieldset>
<legend class="checkout-form-panel--padding-fix checkout-form-panel__header h3">Payment</legend>
<div class="checkout-form-panel--padding-fix">
<div class="checkout-form-panel__col-from-medium-12 payment-switch__toggle">
<div>
<div class="billing-address" id="checkout-form-fieldset-billing-address">
<fieldset>
<legend class="h3 checkout-form-panel__header">Billing address</legend>
<div class="feedback-message" id="checkout-fieldset-feedback-billing-address">
<div class="checkout-form-panel__col-from-small-6 input__wrap input__wrap--error">
<label for="billing-address__line-1">Address line 1</label>
<input type="text" id="billing-address__line-1" name="billing-address" data-type="line1" maxlength="35" value=""/>
<div class="input__feedback">Just let us know your address</div>
</div>
<div class="checkout-form-panel__col-from-small-6 last input__wrap">
<div class="checkout-form-panel__col-from-small-6 input__wrap">
<div class="checkout-form-panel__col-from-small-6 last input__wrap">
<div class="checkout-form-panel__col-from-small-6 input__wrap">
<div class="checkout-form-panel__col-from-small-6 last input__wrap">

错误信息

错误 - 线程“main”中的异常 org.openqa.selenium.NoSuchElementException:没有这样的元素:无法 定位元素: {"method":"xpath","selector":".//fieldset[.//input[@id='billing-address__line-1']]"

【问题讨论】:

  • 错误是什么?
  • 错误 - 线程“main”中的异常 org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“。//字段集[.//input[@id='billing-address__line-1']]"}
  • 为什么一定要使用复杂的xpath。如果 id 是唯一的,您可以直接使用它来定位元素。 driver.findElement(By.id("billing-address__line-1"))

标签: selenium selenium-webdriver xpath css-selectors webdriver


【解决方案1】:

您可以简单地使用 id 来查找 WebElement。试试:

driver.findElement(By.id("billing-address__line-1"))

如果你想使用xpath,它应该如下使用:

driver.findElement(By.xpath("//fieldset//input[@id='billing-address__line-1']")

【讨论】:

    【解决方案2】:

    要在与&lt;label&gt; 地址行 1 关联的输入文本字段中发送字符序列,您可以使用以下任一方法Locator Strategies:

    • cssSelector

      driver.findElement(By.cssSelector("label[for='billing-address__line-1']")).sendKeys("learnerhere");
      
    • xpath

      driver.findElement(By.xpath("//label[@for='billing-address__line-1']")).sendKeys("learnerhere");
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-27
      • 1970-01-01
      • 1970-01-01
      • 2018-07-24
      • 1970-01-01
      • 2013-10-08
      • 2019-03-14
      相关资源
      最近更新 更多