为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用Selenium Webdriver单击元素

如何使用Selenium Webdriver单击元素

长风秋雁 2021-05-01 14:53:36
我正在使用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 Weatherdriver.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感到陌生,在这里我被困住了。
查看完整描述

2 回答

?
慕森卡

TA贡献1806条经验 获得超8个赞

如果要针对上述Div,则可以将此Xpath与Explicit wait一起使用。


WebDriverWait wait = new WebDriverWait(driver, 10);

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@ng-click='disableClick || makeEditable()']")));



查看完整回答
反对 回复 2021-05-12
?
慕尼黑8549860

TA贡献1818条经验 获得超11个赞

根据您已共享的HTML,所需的元素是Angular元素,因此必须诱使WebDriverWait使该元素可单击,并且可以使用以下任一解决方案:

  • cssSelector

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.textLabel[title='Waffle Charts - All Variables']"))).click();
  • xpath

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='textLabel' and @title='Waffle Charts - All Variables'][contains(.,'Waffle Charts - All Variables')]"))).click();



查看完整回答
反对 回复 2021-05-12
  • 2 回答
  • 0 关注
  • 202 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信