Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.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/8/selenium/4.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 如何使用SeleniumWeb驱动程序访问数组的第一个元素并在运行时单击相同的元素?_Java_Selenium_Selenium Webdriver - Fatal编程技术网

Java 如何使用SeleniumWeb驱动程序访问数组的第一个元素并在运行时单击相同的元素?

Java 如何使用SeleniumWeb驱动程序访问数组的第一个元素并在运行时单击相同的元素?,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我有一个下拉列表id=“产品尺寸”和项目S、M、L、XL 挑选 s M L 特大号 我使用数组存储这些项目,在运行时我需要访问第一个元素。我面临的问题是,我无法在运行时单击第一个元素。 我将代码编写如下: driver.get(“https://m.staging.karmaloop.com/product/The-Infinity-Tee/407819"); WebElement j=driver.findElement(按.id(“产品大小”); String text=j.getTex

我有一个下拉列表id=“产品尺寸”和项目S、M、L、XL


挑选
s
M
L
特大号
我使用数组存储这些项目,在运行时我需要访问第一个元素。我面临的问题是,我无法在运行时单击第一个元素。 我将代码编写如下:

driver.get(“https://m.staging.karmaloop.com/product/The-Infinity-Tee/407819");
WebElement j=driver.findElement(按.id(“产品大小”);
String text=j.getText();
字符串[]DDLcount=text.split(“\n”);

对于(inti=1;i您提供的代码,您使用的选项选择器无效

它们似乎没有
名称
属性

除了修改循环外,如果不重建DOM,还可以加快操作速度

WebElement selectBox = driver.findElement(By.xpath(Testconfiguration.size_dropdown_10deep));
List<WebElement> options = selectBox.findElements(By.tagName("option"));
for ( WebElement option : options )
{      
    selectBox.click();
    option.click();
}
WebElement selectBox=driver.findElement(By.xpath(Testconfiguration.size\u dropdown\u 10deep));
列表选项=selectBox.findElements(按.tagName(“选项”));
用于(WebElement选项:选项)
{      
选择框。单击();
选项。单击();
}
您可以使用以下三个选项中的任意一个从下拉列表中选择项目

select.selectByIndex(index); // Give Index as parameter
select.selectByValue(value); // Give the value of the option tag 
select.selectByVisibleText(value); // Give the visible text as parameter
WebElement=driver.findElement(By.id(“产品大小”);
选择sel=新选择(元素);
List items=sel.getOptions();
布尔stringExits=false;

对于(int i=0;iDid它是否引发异常?@John Yes它以org.openqa.selenium.NoSuchElementException的形式引发了异常:否element@John是的,它抛出了一个异常,名为org.openqa.selenium.NoSuchElementException:没有这样的元素–@Baburaj使用数组有什么特殊原因吗?使用select类也可以这样做!!让我知道我可以给出相同的代码。我可以知道你为什么要单击它们吗?我有几个问题:1.当我复制你提供的代码时,它在“selectbox”处显示错误。我如何解决这个问题?2.我可以得到该下拉列表中的第一个元素吗?什么错误?findElements会填充按它们在DOM中的构造顺序列出。因此
options.get(0)
应该是下拉列表中的第一个元素如果不需要任何其他选项,您甚至可以运行
selectBox.findElement(By.tagName(“option”);
driver.findElement(By.id(“2119362”);
driver.findElement(By.csselector(“option[value='4']”);
我只是想知道,这个选择框是什么意思??当我粘贴代码时,红色下划线显示在选择框下方,当我悬停鼠标时,错误显示为“选择框无法解决”“。很抱歉……这是因为WebElement selectBox.的第一行代码被注释了!!!现在它正在工作。感谢您的帮助。非常感谢。ByeI已粘贴了代码,但在语句中显示了一些错误。Select sel=new Select(元素)
select.selectByIndex(index); // Give Index as parameter
select.selectByValue(value); // Give the value of the option tag 
select.selectByVisibleText(value); // Give the visible text as parameter
 WebElement element =driver.findElement(By.id("product-size"));
Select sel = new Select(element);
List<WebElement> items = sel.getOptions();
boolean stringExits = false;
for(int i =0; i<items.size(); i++)
{
    String text = items.get(i).getText();
    if(text.equals("S"))
    {
         stringExists = true;
         break;
    }
}

if(stringExits)
{
     System.out.println("The string exists");
}else
{
     System.out.println("The string does not exist");
}