如何使用Selenium Webdriver单击元素

我正在使用Selenium Web驱动程序在google chrome浏览器中自动化Web模块。我已经创建了一个Web元素的相对XPath,但我想单击它,但不幸的是,它无法正常工作,并且给我一个例外,该XPath不在网页上。动态XPath不能正常工作,因为我首先捕获了这一点,这就是为什么我创建了相对XPath的原因。


硒代码:


//Dashboard:

WebElement ele = driver.findElement(By.xpath("//*[@id=\"sidebar\"]/section/ul/li[1]/a"));

Actions action = new Actions(driver);

action.moveToElement(ele).perform(); // Mover Hover     

WebElement ele2 = driver.findElement(By.xpath("//*[@id=\"sidebar\"]/section/ul/li[1]/ul/li[1]/a"));

ele2.click(); // Click onto Weather

driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);

WebElement ele4 = driver.findElement(By.xpath("//*[contains(@id, 'pvExplorationHost')]//li[1]/div"));

ele4.click();

网页上的HTML代码:


<div class="textLabel" ng-click="disableClick || makeEditable()" title="Waffle Charts - All Variables">Waffle Charts - All Variables</div>

请告诉我,我对Selenium Web-Driver感到陌生,在这里我被困住了。


长风秋雁
浏览 203回答 2
2回答

慕森卡

如果要针对上述Div,则可以将此Xpath与Explicit wait一起使用。WebDriverWait wait = new WebDriverWait(driver, 10);wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@ng-click='disableClick || makeEditable()']")));

慕尼黑8549860

根据您已共享的HTML,所需的元素是Angular元素,因此必须诱使WebDriverWait使该元素可单击,并且可以使用以下任一解决方案:cssSelector:new&nbsp;WebDriverWait(driver,&nbsp;20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.textLabel[title='Waffle&nbsp;Charts&nbsp;-&nbsp;All&nbsp;Variables']"))).click();xpath:new&nbsp;WebDriverWait(driver,&nbsp;20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='textLabel'&nbsp;and&nbsp;@title='Waffle&nbsp;Charts&nbsp;-&nbsp;All&nbsp;Variables'][contains(.,'Waffle&nbsp;Charts&nbsp;-&nbsp;All&nbsp;Variables')]"))).click();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java