【问题标题】:Button click in selenium webdriver - javaselenium webdriver中的按钮单击 - java
【发布时间】:2016-01-30 15:40:39
【问题描述】:

尝试单击 Web 应用程序中的按钮。我正在用 chrome 打开下面的页面,该页面正在打开,但试图单击页面内的按钮,但这是不可能的。

package example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Example {
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver",
                "E:/chromedriver_win32/chromedriver.exe");
        WebDriver driver = new ChromeDriver();


        driver.get("http://localhost:4848/sense/app/C%3A%5CUsers%5Cpramod.STAR%5CDocuments%5CQlik%5CSense%5CApps%5Cdailyreportsample/sheet/PTdBnn/state/analysis");
        driver.findElement(
                By.cssSelector("div.qui-buttonset-left ng-scope button.qui-popover-button.qui-dropdown.ng-scope.ng-isolate-scope.qui-button"))
                .click();

        WebElement element = driver.findElement(By.name("a"));

        element.submit();
        driver.quit();
    }

}

我尝试使用 xpath 也没有得到。当我单击按钮时,我的 xpath 助手会显示以下查询。

/html[@class='touch-off']/body[@class='qv-client qv-story-disabled qv-sheet-enabled qv-view-sheet']/div[@class='qv -panel-wrap']/div[@id='qv-toolbar-container']/div[@class='ng-scope']/div[@class='qui-toolbar']/div[@class= 'qui-buttonset-left ng-scope']/button[@class='qui-popover-button qui-dropdown ng-scope ng-isolate-scope qui-button'][2]

HTML 代码片段:按钮

<button class="qui-popover-button qui-dropdown ng-scope ng-isolate-scope qui-button" tid="2fedac" data-ng-disabled="quiModel.isDisabled()" data-ng-class="buttonClasses" data-icon="toolbar-menu" q-title-translation="Toolbar.Menu" data-qva-activate="onClick()" qui-model="globalMenuButton" ng-if="!isSmallDevice" title="Menu"></button>

【问题讨论】:

  • 能否提供网页元素的 HTML 代码 sn-p。
  • 其实这是一个用于业务分析的网络应用。我正在使用 localhost:4848 使用 Web 浏览器打开我无法查看页面源代码。
  • 试试 ==> By.cssSelector("div.qui-toolbar > div > button:nth-child(2)")
  • 运气不好不能工作。线程“主”org.openqa.selenium.WebDriverException 中的异常:未知错误:元素在点 (86、22) 处不可点击。其他元素会收到点击:
    ...

标签: java selenium-webdriver


【解决方案1】:

尝试在页面加载后添加等待(隐式/显式)并 你应该修改你的css选择器如下:

driver.findElement(By.cssSelector("div[class^='qui-toolbar']>div>button")).click();

【讨论】:

  • 你能解释一下我如何使用等待(隐式/显式)
  • 隐式等待:: driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);这里 10 是秒数。显式等待:WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));更多信息请参考“seleniumhq.org/docs/04_webdriver_advanced.jsp
  • 我也使用了下面的代码,但没有工作。 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  • 你能告诉我抛出了什么异常吗?控制台中一定会显示一些异常或错误消息。
  • 线程 "main" org.openqa.selenium.WebDriverException 中的异常:未知错误:元素在点 (86, 22) 处不可点击。
【解决方案2】:

尝试以下 XPATH 定位器。

driver.findElement(By.xpath("//button[@class='qui-popover-button qui-dropdown ng-scope ng-isolate-scope qui-button']")).click();

或者

driver.findElement(By.xpath("//button[@title='Menu']")).click();

【讨论】:

    【解决方案3】:

    这对我有用。

    WebElement menuButton = driver.findElement(By
                        .cssSelector("button.qui-popover-button:nth-child(2)"));
                menuButton.click();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      相关资源
      最近更新 更多