有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

SeleniumWebDriverJava中的按钮单击

正在尝试单击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='qui-toolbool']/div[@class='qui-buttonset-left-ng-ng-ng-scope']]/button[@class='qui-popover-button-qui-button-down-down-scope-ng-ng-ng-scope-ng-qui-button']

HTML代码片段:of按钮

<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>

共 (3) 个答案

  1. # 1 楼答案

    请尝试使用以下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();
    
  2. # 2 楼答案

    这对我有用

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

    尝试在加载页面后添加等待(隐式/显式),然后重试 您应该修改css选择器,如下所示:

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