Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
单击使用Selenium with Cucumber for W3Schools站点的元素时出现无效的选择器异常_Selenium_Selenium Webdriver_Xpath_Css Selectors_Cucumber Java - Fatal编程技术网

单击使用Selenium with Cucumber for W3Schools站点的元素时出现无效的选择器异常

单击使用Selenium with Cucumber for W3Schools站点的元素时出现无效的选择器异常,selenium,selenium-webdriver,xpath,css-selectors,cucumber-java,Selenium,Selenium Webdriver,Xpath,Css Selectors,Cucumber Java,我正在使用Cucumber for W3Schools站点尝试以下Selenium代码。当我单击“自己尝试”按钮时,它会导航到打开不同窗口的另一页,窗口控件也会转到打开的新窗口。因此,在打开的新窗口中,如果我单击run按钮,它会抛出一个异常: 无效选择器 代码: //单击“自己尝试”按钮 @FindBy(how=how.XPATH,使用=“//*[@id=\“main\”]/div[4]/p/a”) 私密的网络元素; public void TryItYourSelfClick() { TryI

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

无效选择器

代码:

//单击“自己尝试”按钮
@FindBy(how=how.XPATH,使用=“//*[@id=\“main\”]/div[4]/p/a”)
私密的网络元素;
public void TryItYourSelfClick()
{
TryItYourself.click();
}
//现在,一个新窗口打开,我想点击Run按钮
@FindBy(how=how.LINK_TEXT,使用=“Run>>”)
私有WebElement RunButton;
public void RunClick()
{
运行按钮。单击();
}
调用Run方法
@然后(“^a新窗口应显示$”)
公共作废新窗口应出现(){
System.out.println(“单击前运行按钮”);
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
obj1.RunClick();
System.out.println(“单击后运行”);
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
Set handle=driver.getWindowHandles();
字符串firstWinHandle=driver.getWindowHandle();
字符串WinHandle=handle.iterator().next();
if(WinHandle!=firstWinHandle)
{
driver.switchTo()窗口(WinHandle);
System.out.println(“为打开的新窗口工作”);
}
}
为什么会出现此无效选择器异常

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>

运行»
结果大小:433x439

我不熟悉cucumber,但它是否有使用cssSelector的选项

我看了看按钮,发现

body .trytopnav button
作为有效的选择器。

此错误消息

invalid Selector Exception
…表示使用=“Run>>”
how=how.LINK_文本不是有效的选择器

主要问题是元素不是一个
链接\u TEXT
,而是一个
标记,文本为
Run»

解决方案 更改如下:

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

请看这里,我们需要尽可能避免使用>>,因此尝试使用另一个定位器,方法是避免这些

哪一行抛出无效的选择器异常?运行按钮。单击();使用另一个选项更新。让我知道我用其他定位器类尝试过的状态,该定位器类没有此>>无效字符作为定位器。但是,它仍然不工作运行»使用此xpath//a[@id='menuButton']/parent::div/buttonIts仍然无法使用上述xpathexception??请提供完整的例外情况,如果可能的话,您的工作地址
@FindBy(how=How.XPATH,using="//button[contains(.,'Run »')]")
//or
@FindBy(how=How.XPATH,using="//button[contains(.,'Run')]")
private WebElement RunButton;