xpath元素标识 - Cleaner Xpath

时间:2017-10-06 15:44:31

标签: java html css xpath selenium-webdriver

我能够使用直接从代码中复制的实际xpath来识别元素,但是有人可以使用以下代码帮助我编写更简单的xpath吗?以下代码是一个有效的代码!

WebElement oCheckbox = myDriver.findElement(By.xpath(".//*[@id='app']/div[1]/div[2]/div/div/div[2]/div/div[1]/div/div/div[2]/div[2]/table/tbody/tr[1]/td[1]/div/label/div/i"));
WebElement oCheckbox1 = myDriver.findElement(By.xpath(".//*[@id='app']/div[1]/div[2]/div/div/div[2]/div/div[1]/div/div/div[2]/div[2]/table/tbody/tr[2]/td[1]/div/label/div/i"));
oCheckbox.click();
oCheckbox1.click();

HTML:

<head>
<body class="">
<div id="app">
<section class="bottom-padding cf top-padding white-bg">
<div class="container">
<div class="row">
<div class="row">
<div class="col">
<div class="row">
<div class="col-lg-3 push-lg-9 padding-l-r-30 padding-bottom">
<div class="col-lg-9 pull-lg-3 padding-l-r-15">
<div class="row dropshadow">
<div class="col-lg-5 padding-none">
<div class="left-round fixed-height gray-bg">
<div>
<div class="section-title">
<div class="floatThead-wrapper" style="position: relative; clear: both;">
<div class="floatThead-container" style="overflow: hidden; padding-left: 0px; padding-right: 0px; position: absolute; margin-top: 0px; top: 0px; z-index: 1001; will-change: transform; transform: translateX(0px) translateY(0px); left: 0px; width: 410.867px;" aria-hidden="true">
<div class="table-wrapper">
<table class="table protocol-table" style="table-layout: fixed; min-width: 410.867px;">
<colgroup>
<thead>
<tbody>
<tr>
<td>
<div>
<input id="select-pcsl-9777-protocol" type="checkbox"/>
<label for="select-pcsl-9777-protocol">
<div>
<i class="fa fa-check" aria-hidden="true"/>
</div>
<span class="sr-only">Select</span>
</label>

3 个答案:

答案 0 :(得分:0)

试试这个:

SELECT IF(count(id) >= 4, 'YES', 'NO') AS OUTPUT
FROM TABLE_NAME    
WHERE 'true' in (COLUMN1, COLUMN2, ..., COLUMN10);

答案 1 :(得分:0)

我不知道为什么@peter解决方案不起作用但您可以使用 JavascriptExecutor

WebElement element= driver.findElement(By.id("select-pcsl-9777-protocol"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

您也可以使用xpath

WebElement element= driver.findElement(By.xpath("//input[@id='select-pcsl-9777-protocol']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

答案 2 :(得分:0)

正如您所提到的,以下xpath正在发挥作用,但这些是绝对路径:

WebElement oCheckbox = myDriver.findElement(By.xpath(".//*[@id='app']/div[1]/div[2]/div/div/div[2]/div/div[1]/div/div/div[2]/div[2]/table/tbody/tr[1]/td[1]/div/label/div/i"));
WebElement oCheckbox1 = myDriver.findElement(By.xpath(".//*[@id='app']/div[1]/div[2]/div/div/div[2]/div/div[1]/div/div/div[2]/div[2]/table/tbody/tr[2]/td[1]/div/label/div/i"));

这是等效的逻辑xpath

WebElement oCheckbox = myDriver.findElement(By.xpath("//i[@class='fa fa-check']"));

如果你试图点击Select元素,你可以稍微细化一下:

WebElement oCheckbox1 = myDriver.findElement(By.xpath("//i[@class='fa fa-check']/span[@class='sr-only']"));

但是,我对aria-hidden="true"属性有点怀疑,所以一个笨拙的xpath会是:

WebElement oCheckbox2 = myDriver.findElement(By.xpath("//input[@id='select-pcsl-9777-protocol']//following::span[1]))"));