Java 如何在SeleniumWebDriver中单击列表中的菜单
我的代码如下。在这里,我需要单击菜单列表中文本为“生产计划”的元素。代码不起作用,请给出一些建议Java 如何在SeleniumWebDriver中单击列表中的菜单,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我的代码如下。在这里,我需要单击菜单列表中文本为“生产计划”的元素。代码不起作用,请给出一些建议 List<WebElement> list = driver.findElements(By.xpath("html/body/div[4]")); for(WebElement we : list) { if(we.getText().contains("Production Plan")) { we.click(); } List List=driver.findE
List<WebElement> list = driver.findElements(By.xpath("html/body/div[4]"));
for(WebElement we : list) {
if(we.getText().contains("Production Plan")) {
we.click();
}
List List=driver.findElements(By.xpath(“html/body/div[4]”);
for(WebElement-we:list){
如果(we.getText()包含(“生产计划”)){
we.click();
}
html代码是
<div style="position: absolute; visibility: visible; background-color: rgb(255, 255, 255); width: 142px; height: 236px; font-family: Verdana,Arial,Helvetica,sans-serif; font-weight: normal; font-style: normal; font-size: 8pt; z-index: 102; top: 84px; left: 0px;">
<div style="position: absolute; visibility: inherit; overflow: hidden; cursor: default; color: rgb(255, 255, 255); background-color: rgb(25, 63, 148); text-align: left; width: 140px; height: 16px; padding-left: 0px; padding-top: 2px; left: 1px; top: 1px;">Material Group</div>
<div style="position: absolute; visibility: inherit; overflow: hidden; cursor: default; color: rgb(255, 255, 255); background-color: rgb(25, 63, 148); text-align: left; width: 140px; height: 16px; padding-left: 0px; padding-top: 2px; left: 1px; top: 19px;">Material Sub Group</div>
<div style="position: absolute; visibility: inherit; overflow: hidden; cursor: default; color: rgb(255, 255, 255); background-color: rgb(25, 63, 148); text-align: left; width: 140px; height: 16px; padding-left: 0px; padding-top: 2px; left: 1px; top: 37px;">Material</div>
<div style="position: absolute; visibility: inherit; overflow: hidden; cursor: default; color: rgb(255, 255, 255); background-color: rgb(25, 63, 148); text-align: left; width: 140px; height: 16px; padding-left: 0px; padding-top: 2px; left: 1px; top: 55px;">UoM</div>
<div style="position: absolute; visibility: inherit; overflow: hidden; cursor: default; color: rgb(255, 255, 255); background-color: rgb(25, 63, 148); text-align: left; width: 140px; height: 18px; left: 1px; top: 73px;">
<div style="position: absolute; visibility: inherit; overflow: hidden; cursor: default; color: rgb(255, 255, 255); background-color: rgb(25, 63, 148); text-align: left; width: 140px; height: 16px; padding-left: 0px; padding-top: 2px; left: 1px; top: 91px;">Supplier Type</div>
<div style="position: absolute; visibility: inherit; overflow: hidden; cursor: default; color: rgb(255, 255, 255); background-color: rgb(25, 63, 148); text-align: left; width: 140px; height: 16px; padding-left: 0px; padding-top: 2px; left: 1px; top: 109px;">Supplier</div>
<div style="position: absolute; visibility: inherit; overflow: hidden; cursor: default; color: rgb(255, 255, 255); background-color: rgb(25, 63, 148); text-align: left; width: 140px; height: 18px; left: 1px; top: 127px;">
<div style="position: absolute; visibility: inherit; overflow: hidden; cursor: default; color: rgb(255, 255, 255); background-color: rgb(25, 63, 148); text-align: left; width: 140px; height: 16px; padding-left: 0px; padding-top: 2px; left: 1px; top: 145px;">Department</div>
<div style="position: absolute; visibility: inherit; overflow: hidden; cursor: default; color: rgb(255, 255, 255); background-color: rgb(25, 63, 148); text-align: left; width: 140px; height: 16px; padding-left: 0px; padding-top: 2px; left: 1px; top: 163px;">Rejection Reason</div>
<div style="position: absolute; visibility: inherit; overflow: hidden; cursor: default; color: rgb(255, 255, 255); background-color: rgb(25, 63, 148); text-align: left; width: 140px; height: 16px; padding-left: 0px; padding-top: 2px; left: 1px; top: 181px;">Exchange Rate</div>
<div style="position: absolute; visibility: inherit; overflow: hidden; cursor: default; color: rgb(255, 255, 255); background-color: rgb(25, 63, 148); text-align: left; width: 140px; height: 16px; padding-left: 0px; padding-top: 2px; left: 1px; top: 199px;">Standard Yield</div>
<div style="position: absolute; visibility: inherit; overflow: hidden; cursor: default; color: rgb(255, 255, 255); background-color: rgb(25, 63, 148); text-align: left; width: 140px; height: 16px; padding-left: 0px; padding-top: 2px; left: 1px; top: 217px;">Production Plan</div>
材料组
材料分组
材料
计量单位
供应商类型
供应商
部门
拒绝理由
汇率
标准产量
生产计划
看起来您将单击一个div元素,它可能会或可能不会实际执行任何操作,那么“代码不起作用”到底是什么意思?如上所述,请提供HTMLYou正在使用循环,但您不需要它,因为您编写的xpath只会生成一个元素(第四个div)-换句话说,您的概念有问题。很可能您的xpath不正确。