使用Selenium with Cucumber for W3Schools site点击元素的选择器异常无效

时间:2018-06-05 11:33:21

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

我正在尝试使用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 code:

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

3 个答案:

答案 0 :(得分:0)

我不熟悉黄瓜,但它有选择使用cssSelector吗?

我看了一下按钮,找到了

body .trytopnav button

作为有效选择器。

答案 1 :(得分:0)

此错误消息......

invalid Selector Exception

...暗示 how = How.LINK_TEXT,使用=“运行&gt;&gt;”不是有效的选择器。

主要问题是元素不是LINK_TEXT,而是<button>标记,文字为 Run »

解决方案

按如下方式更改Locator Strategy

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

答案 2 :(得分:0)

看这里https://www.seleniumhq.org/exceptions/invalid_selector_exception.jsp我们需要避免使用&gt;&gt;尽可能使用另一个定位器,避免使用这些