Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium WebDriver Java-按标签单击元素不处理某些标签_Java_Selenium_Selenium Webdriver_Webdriver - Fatal编程技术网

Selenium WebDriver Java-按标签单击元素不处理某些标签

Selenium WebDriver Java-按标签单击元素不处理某些标签,java,selenium,selenium-webdriver,webdriver,Java,Selenium,Selenium Webdriver,Webdriver,我正在尝试单击标签旁边的元素。以下是我正在使用的代码: driver.findElement(By.xpath("id(//label[text() = 'LABEL TEXT HERE']/@for)")).click(); 它适用于(全选)&海沃德,但找不到洛杉矶、旧金山或圣何塞 更新: 现在我想这可能是我最好的选择,直到我看到更好的东西。这将允许用户传递完整的字符串,方法中的函数将获取字符串的最后一个单词,并将其插入到包含xpath的文件中 public void subStringLo

我正在尝试单击标签旁边的元素。以下是我正在使用的代码:

driver.findElement(By.xpath("id(//label[text() = 'LABEL TEXT HERE']/@for)")).click();
它适用于(全选)&海沃德,但找不到洛杉矶、旧金山或圣何塞

更新:

现在我想这可能是我最好的选择,直到我看到更好的东西。这将允许用户传递完整的字符串,方法中的函数将获取字符串的最后一个单词,并将其插入到包含xpath的文件中

public void subStringLocationTest(String location) {

    String par = location.substring(location.lastIndexOf(" ") + 1);

    driver.findElement(By.xpath("//label[contains(text(), '" + par + "')]")).click();

}
以下是HTML代码:

<div id="ReportViewer1_ctl04_ctl03_divDropDown" onclick="event.cancelBubble=true;" onactivate="event.cancelBubble=true;" style="border: 1px solid rgb(169, 169, 169); font-family: Verdana; font-size: 8pt; overflow: auto; background-color: window; display: inline; position: absolute; z-index: 11; left: 131px; top: 41px; width: 188px;">
    <span><table cellpadding="0" cellspacing="0" style="background-color:window;">
        <tbody><tr>
            <td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl00" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl00" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnSelectAllClick(this);"><label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl00">(Select All)</label></span></td>
        </tr><tr>
            <td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl02" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl02" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnValidValueClick(this, 'ReportViewer1_ctl04_ctl03_divDropDown_ctl00');"><label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl02">Hayward</label></span></td>
        </tr><tr>
            <td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl03" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl03" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnValidValueClick(this, 'ReportViewer1_ctl04_ctl03_divDropDown_ctl00');"><label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl03">Los Angeles</label></span></td>
        </tr><tr>
            <td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl04" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl04" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnValidValueClick(this, 'ReportViewer1_ctl04_ctl03_divDropDown_ctl00');"><label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl04">San Francisco</label></span></td>
        </tr><tr>
            <td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl05" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl05" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnValidValueClick(this, 'ReportViewer1_ctl04_ctl03_divDropDown_ctl00');"><label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl05">San Jose</label></span></td>
        </tr>
    </tbody></table><input type="hidden" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl01$HiddenIndices" id="ReportViewer1_ctl04_ctl03_divDropDown_ctl01_HiddenIndices" value=""></span>
</div>

(全选)
海沃德
洛杉矶
旧金山
圣何塞
使用以下XPath:

//label[text()='(Select All)']
//label[text()='Hayward']
//label[text()='Los Angeles']
//label[text()='San Francisco']
//label[text()='San Jose']

您还可以执行以下操作:

"//label[contains(text(), 'TEXT_TO_FIND')]"

尝试使用id而不是xpath:

//for Select All
driver.findElement(By.id("ReportViewer1_ctl04_ctl03_divDropDown_ctl00")).click();
//for Hayward
driver.findElement(By.id("ReportViewer1_ctl04_ctl03_divDropDown_ctl02")).click();
//for Los Angeles
driver.findElement(By.id("ReportViewer1_ctl04_ctl03_divDropDown_ctl03")).click();
//for San Francisco
driver.findElement(By.id("ReportViewer1_ctl04_ctl03_divDropDown_ctl04")).click();
//for San Jose
driver.findElement(By.id("ReportViewer1_ctl04_ctl03_divDropDown_ctl05")).click();
如果仍然存在问题或id正在动态更改,则可以使用xpath,如:

//label[contains(.,'(Select All)')]
//label[contains(.,'Hayward')]
//label[contains(.,'Los Angeles')]
//label[contains(.,'San Francisco')]
//label[contains(.,'San Jose')]
试试CSS:

driver.findelelement(By.CSS(“label:contains('Partial或full text')”);

试试这个,也许是一个好的起点:

System.out.print(driver.findElement(By.xpath("//div[@id='ReportViewer1_ctl04_ctl03_divDropDown']/span/table/tbody/tr[" + i +"]/td/span/label")).getText());
driver.findElement(By.xpath("//div[@id='ReportViewer1_ctl04_ctl03_divDropDown']/span/table/tbody/tr[" + i +"]/td/span/label")).click();

i = 3; //Los Angeles
i = 4; //San Francisco

我希望,我的xpath是正确的。

使用下面的代码单击标签或选择标签

driver.findElement(By.xpath("//label[@for='your id which you want to click']")).click();

您可以使用xpath定位器。例如:

d.findElement(By.xpath("//...../label[contains(@for,'ReportViewer1_ctl04_ctl03_divDropDown_ctl02')]")).click();

有相同的问题。只有前两个工作//label[text()='(全选)]//label[text()='Hayward']仍然无法单击LA、SF或SJ。您使用哪个浏览器运行?我个人在FF和Chrome上运行过,能够使用发布的XPath单击所有标签。可能您有多个同名的标签(LA、SF或SJ)在页面中。在这种情况下,请发布完整的html源代码或指向html页面的链接。我正在使用firefox-下面是一个我无法找到元素的错误示例:{“方法”:“xpath”,“选择器”:“//label[text()='San Jose']”我检查过页面,标签不存在于页面的任何其他地方。当你传递整个字符串时,这个标签不起作用。例如,“旧金山”会失败,但是“弗朗西斯科”会起作用。因为这是一个带字符串位置的方法。当他们调用方法时,他们通过了整个位置(圣若泽,旧金山,洛杉矶)。由于contains似乎工作得最好,我继续创建了一个方法,该方法具有将完整字符串转换为部分字符串的函数。请参阅上面的方法-“subStringLocationTest”,您需要转义空格。尝试使用&;我实际上已经尝试过此方法,但仍然无法找到元素。是否尝试过此driver.findElement(By.cssSelector(“#ReportViewer1_ctl04_ctl03_divDropDown>span>tbody>tr:nth类型(如果td u愿意,则为数字)”)。单击();不能使用数字或ID。它必须能够通过选择来完成。我们正在构建一个前端,允许最终用户(业务)输入变量。我们不能要求最终用户输入数字。不能使用ID,因为用户将通过位置(LA、SJ等)作为一个变量。我不想创建if-else语句,因为我可能有100个选项。那么我提供的Xpath呢。这些对您有用吗?不,Xpath不适用于以下内容://label[contains(,'Los Angeles')]//label[contains(,'San Francisco')]//label[contains(,'San Jose')]仍然无法找到洛杉矶,旧金山,圣约瑟使用CSS无法找到任何东西。与XPath,我至少能够获得(选择所有)和Hayward。
d.findElement(By.xpath("//...../label[contains(@for,'ReportViewer1_ctl04_ctl03_divDropDown_ctl02')]")).click();