请帮帮我。
无法选择元素的原因是
当我移动/点击下拉列表id='rfdSubMenu1396697749612'
显示可见性:可见。如果我将光标移离下拉列表可见性:隐藏。
现在,当我运行下面的代码时,它会抛出没有这样的元素id =' rfdSubMenu1396697749612'所以如何克服这个问题,使其可见并选择一个选项。
junit代码来自Selnium IDE
driver.findElement(By.id("ctl00_InventoryContent_btnAddSystem")).click();
driver.findElement(By.id("ctl00_InventoryContent_txtAssetName")).clear();
driver.findElement(By.id("ctl00_InventoryContent_txtAssetName")).sendKeys("ASDE346");
driver.findElement(By.id("ctl00_InventoryContent_txtControlNumber")).clear();
driver.findElement(By.id("ctl00_InventoryContent_txtControlNumber"))
.sendKeys("ASDE346");
driver.findElement(By.cssSelector("span.rfdSelectText")).click();
driver.findElement(By.xpath("//div[@id='rfdSubMenu1396538734351']/ul/
li[2]")).click();
new Select(driver.findElement(By.id("ctl00_InventoryContent_ddlAssetType")))
.selectByVisibleText("Networking");
driver.findElement(By.cssSelector("#Skinnedctl00_InventoryContent_ddlAssetStatus >
span.rfdSelectOuter > span.rfdSelectText")).click();
driver.findElement(By.xpath("//div[@id='rfdSubMenu1396538761911']/ul/li[3]"))
.click();
new Select(driver.findElement(By.id("ctl00_InventoryContent_ddlAssetStatus")))
.selectByVisibleText("InActive");
Junit代码中的更改
driver.findElement(By.cssSelector("span.rfdSelectText")).click();
// No such Element Present Error occurs in next line
WebElement dis = driver.findElement(By.cssSelector("html.RadForm body
div#rfdSubMenu1396685935145.rfdSelectBox"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("rfdSelect_selected.click()" , dis);
new Select(driver.findElement(By.id("ctl00_InventoryContent_ddlAssetStatus")))
.selectByVisibleText("InActive");
HTML CODE
<div id="rfdSubMenu1396697749612" class="rfdSelectBox rfdSelectBox_Default"
style="overflow-y: auto; width: 149px; position: absolute; visibility: hidden; left:
885px; top: 277px; display: none;">
答案 0 :(得分:0)
如异常中所述,元素不可见,因此Selenium无法与之交互。确保元素可见或触发事件,使下拉列表可见。
答案 1 :(得分:0)
正如karna所提到的,必须触发一些事件才能使下拉显示,如果您粘贴了确切的html代码,可能会有悬停或点击事件以使下拉列表可见。您可以尝试click()事件:
new WebDriverWait(driver,60).until(ExpectedConditions.visibilityOfElementLocated(By.id("industryType"))).click();
new WebDriverWait(driver,60).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#industryType option[value='Nt']"))).click();
new WebDriverWait(driver,60).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#industryType option[value='Nt']"))).click();
答案 2 :(得分:0)
Actions act = new Actions(driver);
act.moveToElement(driver.findElement(By.xpath("//html/body/div/ul/li[2]"))).perform();
act.click().perform();