Selenium – 如何获取下一个兄弟元素?

huangapple go评论89阅读模式
英文:

Selenium - How to get following sibling?

问题

所以我想要定位出在标签“First Name”之后出现的列表中的文本框,然后向该文本框发送一个名字,但似乎无法定位到这个文本框...

我尝试过的:

	    WebElement firstNameLocTry = driver.findElement(By.xpath("//label[text()='First Name']/following-sibling::div"));
	    firstNameLocTry.sendKeys(firstName);

列表项的HTML结构如下:

<li class="WPTO WKVO" role="presentation" data-automation-id="formLabelRequired">

<div class="WBUO WETO WDUO">
<label id="56$551056--uid24-formLabel" data-automation-id="formLabel" for="56$551056--uid24-input">First Name</label>
<div class="WEUO wd-c8594868-6b31-4526-9dda-7d146648964b" aria-hidden="true">First Name</div>
</div>

<div data-automation-id="decorationWrapper" id="56$551056" class="WFUO">
<div class="WICJ">
<div class="WMP2 textInput WLP2 WJ5" data-automation-id="textInput" id="56$551056--uid24" data-metadata-id="56$551056" style="visibility: visible;">
<input type="text" class="gwt-TextBox WEQ2" data-automation-id="textInputBox" tabindex="0" role="textbox" id="56$551056--uid24-input" aria-invalid="false" aria-required="true">
</div>
</div>
</div>

</li>

为什么我的 sendKeys 操作会导致 "Element not interactable" 错误呢?

英文:

So I want to target a textbox that appears in a list that comes after the label "First Name" and send a first name to the box, but can't seem to be able to target the textBox...

What I've tried:

	    WebElement firstNameLocTry = driver.findElement(By.xpath(&quot;//label[text()=&#39;First Name&#39;]/following-sibling::div&quot;));
	    firstNameLocTry.sendKeys(firstName);

What the li look like:

&lt;li class=&quot;WPTO WKVO&quot; role=&quot;presentation&quot; data-automation-id=&quot;formLabelRequired&quot;&gt;

&lt;div class=&quot;WBUO WETO WDUO&quot;&gt;
&lt;label id=&quot;56$551056--uid24-formLabel&quot; data-automation-id=&quot;formLabel&quot; for=&quot;56$551056--uid24-input&quot;&gt;First Name&lt;/label&gt;
&lt;div class=&quot;WEUO wd-c8594868-6b31-4526-9dda-7d146648964b&quot; aria-hidden=&quot;true&quot;&gt;First Name&lt;/div&gt;
&lt;/div&gt;

&lt;div data-automation-id=&quot;decorationWrapper&quot; id=&quot;56$551056&quot; class=&quot;WFUO&quot;&gt;
&lt;div class=&quot;WICJ&quot;&gt;
&lt;div class=&quot;WMP2 textInput WLP2 WJ5&quot; data-automation-id=&quot;textInput&quot; id=&quot;56$551056--uid24&quot; data-metadata-id=&quot;56$551056&quot; style=&quot;visibility: visible;&quot;&gt;
&lt;input type=&quot;text&quot; class=&quot;gwt-TextBox WEQ2&quot; data-automation-id=&quot;textInputBox&quot; tabindex=&quot;0&quot; role=&quot;textbox&quot; id=&quot;56$551056--uid24-input&quot; aria-invalid=&quot;false&quot; aria-required=&quot;true&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;/li&gt;

Any reason my sendKeys just leads to Element not interactable?

答案1

得分: 1

以下是翻译好的内容:

附上的HTML代码产生以下输出

给定的XPath指向第二个“名字”Div [下图],当您执行sendKeys时,很明显会抛出“Element not interactable”错误。

尝试以下两个Xpath:

  1. //label[text()='名字']//parent::div/following-sibling::div
  2. //label[text()='名字']//parent::div/following-sibling::div//input
英文:

The attached HTML code Produce below out put

Selenium – 如何获取下一个兄弟元素?

With the given XPath points to the second "First Name" Div [below pic], when you perform sendKeys, it is obvious that the error "Element not interactable" will be thrown.

Selenium – 如何获取下一个兄弟元素?

Try with below two Xpaths,

1. //label[text()=&#39;First Name&#39;]//parent::div/following-sibling::div
2. //label[text()=&#39;First Name&#39;]//parent::div/following-sibling::div//input

答案2

得分: 0

请使用以下 XPath:

//div[text()='名字']

或尝试:

//*[contains(text(),'名字')]
英文:

Please use following xpath:

//div[text()=&#39;First Name&#39;]

or try:

//*[contains(text(),&#39;First Name&#39;)]

huangapple
  • 本文由 发表于 2020年9月24日 12:35:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64039629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定