【问题标题】:Invalid selector exception for clicking an element using Selenium with Cucumber for W3Schools site使用 Selenium 和 Cucumber for W3Schools 站点单击元素的无效选择器异常
【发布时间】:2018-06-08 13:51:24
【问题描述】:

我正在使用 Cucumber for W3Schools 网站尝试使用以下 Selenium 代码。当我单击“自己尝试”按钮时,它会导航到打开不同窗口的另一个页面,并且 窗口控件也会转到打开的新窗口。因此,在打开的新窗口中,如果我单击运行按钮,则会引发异常:

无效的选择器

代码:

//This clicks on the Try it yourself button
@FindBy(how=How.XPATH,using="//*[@id=\"main\"]/div[4]/p/a")
    private WebElement TryItYourself;
public void TryItYourSelfClick()
        {
            TryItYourself.click();  
        }
//Now,a new window opens up where I want to click on Run Button
    @FindBy(how=How.LINK_TEXT,using="Run >>")
    private WebElement RunButton;
public void RunClick()
        {
            RunButton.click();
        }

Calling Run Method
@Then("^a new window should appear$")
    public void a_new_window_should_appear() {
        System.out.println("Run button before Clicking");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        obj1.RunClick();
        System.out.println("Run after clicking");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
         Set <String> handle=driver.getWindowHandles();
         String firstWinHandle=driver.getWindowHandle();
         String WinHandle=handle.iterator().next();
         if(WinHandle!=firstWinHandle)
         {
             driver.switchTo().window(WinHandle);
             System.out.println("Working for new window opened");
         }

}

为什么会出现这个无效的选择器异常?

HTML 代码:

<div class="w3-bar w3-light-grey" style="border-top:1px solid #f1f1f1;overflow:auto">
  <a id="tryhome" href="https://www.w3schools.com" target="_blank" title="w3schools.com Home" class="w3-button w3-bar-item topnav-icons fa fa-home" style="font-size:28px;color:#999999;margin-top:-2px"></a>
  <a href="javascript:void(0);" onclick="openMenu()" id="menuButton" title="Open Menu" class="w3-dropdown-click w3-button w3-bar-item topnav-icons fa fa-menu" style="font-size:28px;color:#999999;margin-top:-2px"></a>
  <a href="javascript:void(0);" onclick="click_savebtn()" title="Save" class="w3-button w3-bar-item topnav-icons fa fa-save" style="font-size:28px;color:#999999;margin-top:-2px"></a>
  <a href="javascript:void(0);" onclick="restack(currentStack)" title="Change Orientation" class="w3-button w3-bar-item topnav-icons fa fa-rotate" style="font-size:28px;color:#999999;margin-top:-2px"></a>
  <button class="w3-button w3-bar-item w3-green w3-hover-white w3-hover-text-green" onclick="submitTryit(1)">Run »</button>
  <span class="w3-right w3-hide-medium w3-hide-small" style="padding:8px 8px 8px 8px;display:block"></span>
  <span class="w3-right w3-hide-small" style="padding:8px 0;display:block;float:right;"><span id="framesize">Result Size: <span>433 x 439</span></span></span>
</div>

【问题讨论】:

  • 哪一行抛出invalid Selector Exception
  • RunButton.click();

标签: selenium selenium-webdriver xpath css-selectors cucumber-java


【解决方案1】:

我对黄瓜不熟悉,但是它有使用cssSelector的选项吗?

我看了看按钮,发现

body .trytopnav button

作为一个有效的选择器。

【讨论】:

    【解决方案2】:

    此错误消息...

    invalid Selector Exception
    

    ...暗示 how=How.LINK_TEXT,using="Run >>" 不是有效的选择器。

    主要问题是元素不是LINK_TEXT,而是&lt;button&gt; 标签,其文本为Run »

    解决方案

    如下更改Locator Strategy

    @FindBy(how=How.XPATH,using="//button[contains(.,'Run »')]")
    //or
    @FindBy(how=How.XPATH,using="//button[contains(.,'Run')]")
    private WebElement RunButton;
    

    【讨论】:

    • 用另一个选项更新。让我知道状态
    【解决方案3】:

    看这里https://www.seleniumhq.org/exceptions/invalid_selector_exception.jsp我们需要尽量避免使用>>,所以尽量避免使用其他定位器

    【讨论】:

    • 我曾尝试使用其他没有此 >> 无效字符作为定位器的定位器类。但仍然无法正常工作
    • 使用这个 xpath //a[@id='menuButton']/parent::div/button
    • 它仍然无法与上述 xpath 一起使用
    • 异常??如果可能的话,请提供完整的例外情况您正在工作的网址
    猜你喜欢
    • 2021-10-21
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 2011-12-10
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    相关资源
    最近更新 更多