Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.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
Java Selenium:从数组中的字符串匹配xpath_Java_Arrays_Selenium_Webdriver - Fatal编程技术网

Java Selenium:从数组中的字符串匹配xpath

Java Selenium:从数组中的字符串匹配xpath,java,arrays,selenium,webdriver,Java,Arrays,Selenium,Webdriver,使用Selenium WebDriver,我试图通过从Excel文件读取的字符串来选择网页上的下拉菜单元素: Xls_Reader data = new Xls_Reader("src//com//testexcel//data//data3.xlsx"); String values = data.getCellData("DropList", "Index_Value", 2); String selections[] = values.spl

使用Selenium WebDriver,我试图通过从Excel文件读取的字符串来选择网页上的下拉菜单元素:

        Xls_Reader data = new Xls_Reader("src//com//testexcel//data//data3.xlsx");
        String values = data.getCellData("DropList", "Index_Value", 2);
        String selections[] = values.split(",");
它们的形式有:建筑、工程、法律等

我尝试选择的每个元素如下所示:

<div class="ui-dropdownchecklist-item ui-state-default" style="white-space: nowrap;">
<input id="ddcl-selInd-i3" class="active" type="checkbox" tabindex="0" index="3" value="11">
<label class="ui-dropdownchecklist-text" for="ddcl-selInd-i3" style="cursor: default;">Construction</label>
</div>

<div class="ui-dropdownchecklist-item ui-state-default" style="white-space: nowrap;">
<input id="ddcl-selInd-i5" class="active" type="checkbox" tabindex="0" index="5" value="03">
<label class="ui-dropdownchecklist-text" for="ddcl-selInd-i5" style="cursor: default;">Engineering</label>
</div>
代码如下:

package com.selftechy.parameterization;

import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class JobServe {

    static WebDriver driver = new FirefoxDriver();

    public static void main(String[] args) throws InterruptedException {


        driver.get("https://www.jobserve.com/gb/en/Candidate/Home.aspx");
        driver.findElement(By.xpath(".//*[@id='ddcl-selInd']/span")).click();

        readExcelWords();


    public static void readExcelWords() {
        Xls_Reader data = new Xls_Reader("src//com//testexcel//data//data3.xlsx");
        String values = data.getCellData("DropList", "Index_Value", 2);
        String selections[] = values.split(",");

//help  
List<WebElement> iList = driver.findElements(By.xpath("//*[@id='ddcl-selInd-ddw']"));
    for (int i=0; i<selections.length; i++) {
    driver.findElement(By.xpath("//*[text()='" + selections[i] + "']")).click();

我知道xpath是错误的,可能是我处理数据类型的方式。我需要一种基于数组值进行xpath选择的方法。我对Java和Selenium比较陌生,希望能得到一些帮助。

我建议尝试使用名称和用法

findElement(By.name("xyz"));
在输入代码中使用name属性之后

差不多-

<div class="ui-dropdownchecklist-item ui-state-default" style="white-space: nowrap;">
<input name ="Construction" id="ddcl-selInd-i3" class="active" type="checkbox" tabindex="0" index="3"value="11">
<label class="ui-dropdownchecklist-text" for="ddcl-selInd-i3" style="cursor: default;">Construction</label>
</div>

我可以使用类似于上面给出的伪代码使用此方法查找元素。

我建议尝试使用名称和用法

findElement(By.name("xyz"));
在输入代码中使用name属性之后

差不多-

<div class="ui-dropdownchecklist-item ui-state-default" style="white-space: nowrap;">
<input name ="Construction" id="ddcl-selInd-i3" class="active" type="checkbox" tabindex="0" index="3"value="11">
<label class="ui-dropdownchecklist-text" for="ddcl-selInd-i3" style="cursor: default;">Construction</label>
</div>
我可以使用上面给出的伪代码,用这种方法找到一个元素。

试试看

findElement(By.xpath("//label[.='xyz']/../input"));
其中xyz是建筑、工程等的一种

findElement(By.xpath("//label[.='xyz']/../input"));

其中,xyz是建筑、工程等领域的一家公司,它不起作用,但谢谢。我尝试了:driver.findElementBy.nameEngineering.click;这使得org.openqa.selenium.NoSuchElementException:无法定位元素:{method:name,selector:Engineering}有什么想法吗?嗨!我编辑了将name属性添加到输入标记的代码。这是我的工作与类似的代码。让我们知道它是否适合您这很有效,非常感谢!我想进一步了解如何像这样构造XPath。Firebug有帮助,但它无法提供此格式-“/../input”例如。欢迎您提出任何建议……很高兴知道它是有效的。即使是我也不擅长格式化xpath。我使用XPathFinder作为Firebug的附加组件来查找我的Xpath。这些xpath有时需要进行一些编辑,以与我在JUnit4中导出IDE测试用例时生成的xpath格式相匹配。我建议您单独提出一个问题,让其他人有效地回答,并在此处为寻求解决方案的其他用户接受任何答案:如果它有效,你可能愿意接受其他人知道它有效的答案!没用,但谢谢。我尝试了:driver.findElementBy.nameEngineering.click;这使得org.openqa.selenium.NoSuchElementException:无法定位元素:{method:name,selector:Engineering}有什么想法吗?嗨!我编辑了将name属性添加到输入标记的代码。这是我的工作与类似的代码。让我们知道它是否适合您这很有效,非常感谢!我想进一步了解如何像这样构造XPath。Firebug有帮助,但它无法提供此格式-“/../input”例如。欢迎您提出任何建议……很高兴知道它是有效的。即使是我也不擅长格式化xpath。我使用XPathFinder作为Firebug的附加组件来查找我的Xpath。这些xpath有时需要进行一些编辑,以与我在JUnit4中导出IDE测试用例时生成的xpath格式相匹配。我建议您单独提出一个问题,让其他人有效地回答,并在此处为寻求解决方案的其他用户接受任何答案:如果它有效,你可能愿意接受其他人知道它有效的答案!