英文:
Selenium | Element not interactable error: Explored all the options of stack overflow
问题
我正在尝试一次性获取网页中的所有下拉菜单,并从中选择一个值。
我附上了一个代码片段,该代码片段获取了网页上引导式下拉菜单中位于<ul>标签下的所有下拉菜单。
我想要访问每个<ul>标签下的<li>标签下的子元素,并单击其中任何一个子元素。
我附上了从网站上截取的屏幕截图。
尽管它是可点击的元素,但它总是显示元素不可交互。
请帮忙解决。
[应用程序屏幕截图][1]
**代码:**
```java
List<WebElement> dropDowns = webDriver.findElements(By.xpath("//ul[contains(@class,'dropdown')]"));
try{Thread.sleep(5000);}catch (Exception e){};
for(WebElement webElement : dropDowns){
try{
List<WebElement> elementList = webElement.findElements(By.xpath("//ul[contains(@class,'dropdown')]//li"));
for (int i = 0 ; i < elementList.size();i++){
elementList.get(i).click();
Thread.sleep(3000);
}
}
catch (Exception e){
System.out.println("-----------Error----------");
continue ;
}
}
try{Thread.sleep(10000);}
catch (Exception e){}
webDriver.quit();
}
<details>
<summary>英文:</summary>
I am trying to get all drop downs from a web page and select a value from them in one go.
I have attached a code snippet which gets all the dropdowns which are bootstrapped and under <ul> tag on the web page.
I want to access children of each ul tag which are under li tag and click on any of those children.
I am attaching the screen shot taken from web site.
It always says element not interactable eventhough it is clikable element.
Please help.
[Application screenshot][1]
**Code:**
List<WebElement> dropDowns = webDriver.findElements(By.xpath("//ul[contains(@class,'dropdown')]"));
try{Thread.sleep(5000);}catch (Exception e){};
for(WebElement webElement : dropDowns){
try{
List<WebElement> elementList = webElement.findElements(By.xpath("//ul[contains(@class,'dropdown')]//li"));
for (int i = 0 ; i < elementList.size();i++){
elementList.get(i).click();
Thread.sleep(3000);
}
}
catch (Exception e){
System.out.println("-----------Error----------");
continue ;
}
}
try{Thread.sleep(10000);}
catch (Exception e){}
webDriver.quit();
}
[1]: https://i.stack.imgur.com/9E6d8.png
</details>
# 答案1
**得分**: 0
我在你的代码中看到以下问题。
- 你试图从`dropDowns`列表中使用`webElement`,但如果在for循环中使用`webElement`,会导致元素失效的异常。
- 由于没有根据索引获取下拉菜单,你的代码会在每次操作中始终在第一个下拉菜单上执行操作。
- 你提到你想在列表中选择一个项目,但你却在每个下拉菜单项上点击。
请尝试使用以下逻辑。
```java
int dropDowns = webDriver.findElements(By.xpath("//ul[contains(@class,'dropdown')]")).size();
try { Thread.sleep(5000); } catch (Exception e) {};
JavascriptExecutor js = (JavascriptExecutor) webDriver;
for (int dropdownIndex = 0; dropdownIndex < dropDowns; dropdownIndex++) {
WebElement dropdown = webDriver.findElements(By.xpath("//ul[contains(@class,'dropdown')]")).get(dropdownIndex);
try {
List<WebElement> elementList = dropdown.findElements(By.xpath(".//li"));
for (int i = 0; i < elementList.size(); i++) { // 不确定你是否真的想要点击下拉菜单中的每个项目,因此没有修改此部分。
WebElement item = elementList.get(i);
js.executeScript("arguments[0].click()", item);
Thread.sleep(3000);
}
} catch (Exception e) {
System.out.println("-----------Error----------");
continue;
}
}
注意:翻译的内容已经按照你的要求进行了提取和整理。如果你有任何其他问题或需要进一步的帮助,请随时提问。
英文:
I see below issues in your code.
- You are trying to use the
webElement
fromdropDowns
list which will through stale element exception if you usewebElement
in the for loop. - Your code will perform the operation on the first operation on the first dropdwn all the times as you are not getting the downdown based on the index.
- you mentioned you want to select an item in the list but you are clicking on the each item in the dropdown.
Please try with the below logic.
int dropDowns = webDriver.findElements(By.xpath("//ul[contains(@class,'dropdown')]")).size();
try{Thread.sleep(5000);}catch (Exception e){};
JavascriptExecutor js = (JavascriptExecutor) webDriver;
for(int dropdownIndex =0; dropdownIndex < dropDowns; dropdownIndex++){
WebElement dropdown = webDriver.findElements(By.xpath("//ul[contains(@class,'dropdown')]")).get(dropdownIndex);
try{
List<WebElement> elementList = dropdown.findElements(By.xpath(".//li"));
for (int i = 0 ; i < elementList.size();i++){ // not sure if you really want to click each item in the dropdown, hence not modified this part.
WebElement item = elementList.get(i);
js.executeScript("arugments[0].click()",item);
Thread.sleep(3000);
}
}
catch (Exception e){
System.out.println("-----------Error----------");
continue ;
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论