英文:
Selenium: How can I get the checked checkbox from nested input by div and label?
问题
Html block first:
<div class="formClass" style="line-height: 18px; display: block;">
<label id="randomId_34_30078" style="display: none; width:47%; margin-right: 5px;">
<input type="checkbox" value="30078" disabled="">First Text
</label>
<label id="randomId_34_30077" style="display: none; width:47%; margin-right: 5px;">
<input type="checkbox" value="30077" disabled="">Second Text
</label>
<label id="randomId_32_30078" style="display: inline-block; width:47%; margin-right: 5px; vertical-align: top;">
<div style="display: inline-block; width: 10%; float: left;">
<input type="checkbox" value="30078" disabled="" checked="">
</div>
<div style="display: inline-block; width: 90%; float: right;">
First Text
</div>
</label>
<label id="randomId_32_30077" style="display: inline-block; width:47%; margin-right: 5px; vertical-align: top;">
<div style="display: inline-block; width: 10%; float: left;">
<input type="checkbox" value="30077" disabled="">
</div>
<div style="display: inline-block; width: 90%; float: right;">
Second Text
</div>
</label>
</div>
I'd like to get the checkbox of which the value is "30078" in the above HTML section. The id of labels are random, also the value of the input/checkbox. It's a legacy project. I can't modify the structure.
I tried this:
driver.findElement(By.className("formClass"))
.findElement(By.xpath("//label//div[contains(text(), 'First Text')]"))
but this gets two elements.
英文:
Html block first:
<div class="formClass" style="line-height: 18px; display: block;">
<label id="randomId_34_30078" style="display: none; width:47%; margin-right: 5px;">
<input type="checkbox" value="30078" disabled="">First Text
</label>
<label id="randomId_34_30077" style="display: none; width:47%; margin-right: 5px;">
<input type="checkbox" value="30077" disabled="">Second Text
</label>
<label id="randomId_32_30078" style="display: inline-block; width:47%; margin-right: 5px; vertical-align: top;">
<div style="display: inline-block; width: 10%; float: left;">
<input type="checkbox" value="30078" disabled="" checked="">
</div>
<div style="display: inline-block; width: 90%; float: right;">
First Text
</div>
</label>
<label id="randomId_32_30077" style="display: inline-block; width:47%; margin-right: 5px; vertical-align: top;">
<div style="display: inline-block; width: 10%; float: left;">
<input type="checkbox" value="30077" disabled="">
</div>
<div style="display: inline-block; width: 90%; float: right;">
Second Text
</div>
</label>
</div>
I'd like to get the checkbox of which the value is "30078" in the above HTML section.
The id of labels are random, also the value of the input/checkbox. It's a legacy project. I can't modify the structure.
I tried this:
driver.findElement(By.className("formClass"))
.findElement(By.xpath("//label//div[contains(text(), 'First Text')]"))
but this gets two elements.
答案1
得分: 0
如果您需要获取复选框,您需要为<input>标签编写XPATH。
如果您需要为以下内容编写xpath:
<label id="randomId_34_30078" style="display: none; width:47%; margin-right: 5px;">
<input type="checkbox" value="30078" disabled="">First Text
</label>
您可以使用:
//div[@class="formClass"]/label/input[contains(text(),"First Text")]
英文:
If you need to get the checkbox, you need to write the XPATH for the <input> tag.
If you need to write the xpath for below :
<label id="randomId_34_30078" style="display: none; width:47%; margin-right: 5px;">
<input type="checkbox" value="30078" disabled="">First Text
</label>
you can use
//div[@class="formClass"]/label/input[contains(text(),"First Text")]
答案2
得分: 0
虽然有两个带有属性 value="30078"
的元素,但其中只有一个是可见的,而另一个包含属性 style="display: none;"
。
因此,要获取具有设置为 30078 的 value 属性的可见/可交互复选框,您需要使用 WebDriverWait 来等待 elementToBeClickable()
,并且您可以使用以下任一 定位策略:
cssSelector
:
WebElement element = new WebDriverWait(webDriver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label[id^='randomId'] input[value='30078']")));
xpath
:
WebElement element = new WebDriverWait(webDriver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[starts-with(@id, 'randomId')]//input[@value='30078']")));
英文:
Though there are two elements with attribute value="30078"
among them only one is visible while the other one contains the attribute style="display: none;
.
So to get the visible/interactable checkbox with value attribute set as 30078 you need to induce WebDriverWait for the elementToBeClickable()
and you can use either of the following Locator Strategies:
-
cssSelector
:WebElement element = new WebDriverWait(webDriver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label[id^='randomId'] input[value='30078']")));
-
xpath
:WebElement element = new WebDriverWait(webDriver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[starts-with(@id, 'randomId')]//input[@value='30078']")));
References
You can find a couple of relevant discussions in:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论