由以下原因引起:org.openqa.selenium.InvalidSelectorException:

时间:2019-04-26 04:30:53

标签: selenium automation

我是硒自动化测试的新手 硒错误

当我每次遇到错误时都尝试在代码下运行“ org.openqa.selenium.TimeoutException:预期条件失败:等待By.xpath所定位元素的可见性”我不确定xpate会是什么一个请帮助我 为登录应用程序创建了自动化测试用例。

Java:

@Test
    public void enterApplicationCerdentials(String userName, String password) {

 WebDriverWait wait = new WebDriverWait(driver, 50);
 wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//frameset/frame/html/body/div([@class='tintedBox' and @id='loginBox']/table/tbody/tr/td/input[@id='UserID' and name='UserID'])"))).sendKeys(userName);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Password"))).sendKeys("CSRWHKXESB");
 captureScreen();
 }

HTML:

<frameset rows="*, 1">

<frame src="Script/Login.asp?">
#document
<!--Function return the password for the user who logged in through Referral URL. 
          For TAFE user password is not matching as per database value as their URL is dynamic. ENHC0014091
          This function splits the URL of that particular user (8527066229)   only  . -->

<html><head>



<script language="JavaScript">
//<!--
function OnLoad()
{
  setTimeout("CheckFrame()", 4000);
}

function CheckFrame()
{
  with (parent)
  {
    if ((length==0 && document.frmCheck==null) || 
        (length>0 && frames[1].document.frmCheck==null))
    {
      var PathName = window.location.pathname;
      PathName = PathName.substr(0, PathName.substr(1).indexOf("/") + 1);
      location.href = PathName;
    }
  }
}
//-->
</script></head>



<body onload="OnLoad()" topmargin="0" leftmargin="0" bgcolor="#ffffff" text="#000000" link="#000000" vlink="#000000" alink="#000000">

<div id="login">
<img border="0" src= vspace="10" style="display: block;">
<div class="tintedBox" id="InfoStoreBox">
<h1>  <a class="boldLink" href="https://infostore.saiglobal.com" target="_parent"><u>InfoStore</u></a> ?</h1>
<p></p>
<p><a class="boldLink" href="https://infostore.saiglobal.com" target="_parent"></a> .</p>
<p>Not only a shop, <a class="boldLink" href="https://infostore.saiglobal.com" target="_parent"></a> </p>
<p><a href="https://infostore.saiglobal.com" target="_parent"></a></p>
</div>

<div class="tintedBox" id="loginBox">
<h1></h1>

<form name="form" action="Login.asp?" method="post">
  <input type="hidden" name="Autologout" value="false">
  <input type="hidden" name="Division" value="">
  <span class="error"></span>
  <table>
      <tbody><tr>
          <td>User ID</td>
          <td><input type="text" size="30" id="UserID" name="UserID" value=""></td>
      </tr>
      <tr>
          <td>Password</td>
          <td><input type="password" size="30" id="Password" name="Password" value=""></td>
      </tr>
          <tr>
          <td>Last Name/Location</td>
          <td><input type="text" size="30" id="UserName" name="UserName" value=""></td>
      </tr>
  </tbody></table>
  <br>
  <font color="#d12b2c"><nobr>Your Last Name and Location are now mandatory to access the platform, simply</nobr><br>enter the details in field (e.g. Smith/Perth)</font>
  <br><br>
  <div>
    <input type="checkbox" id="SaveLogin" name="SaveLogin" value="SaveLogin"><label for="SaveLogin" id="save">Save my login so you can remember me on my next visit.</label>
  </div>
  <div align="center" style="padding-top:20px">
    <input class="button" type="submit" name="Submit" value="Submit">
    <input class="button" type="reset" name="Clear" value="Clear">
    <input class="button" type="submit" name="Recognize" value="Remember me?">
  </div>
  <div style="padding-top:20px">
    <nobr>Lost password? Contact your internal Administrator or <a class="boldLink" href="mailto:sales@saiglobal.com?subject=Lost SOL password">our Customer Service</a></nobr>
  </div>
</form>
</div>





</div>
<div id="footer" onmouseover="ShowTime()">©2014 SAI Global</div>
<script>
  function ShowTime()
  {
    var spTime = document.getElementById("footer");
    spTime.innerHTML = "©2014 SAI Global - 20190426 12:25:33.292: 172.20.33.12, 203.27.21.6, 59596377";
  }
</script>


</body></html>

错误:

  

org.openqa.selenium.TimeoutException:预期条件失败:   等待By.xpath定位的元素的可见性:   // frameset / frame / html / body / div([@ class ='tintedBox'和   @ id ='loginBox'] / table / tbody / tr / td / input [@ id ='UserID'和   name ='UserID'])(尝试了500毫秒(50秒钟)   间隔)       在org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)       在org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)       在seleniumPages.Page_LoginMyApplication.enterApplicationCerdentials(Page_LoginMyApplication.java:67)       在stepDefinitions.StepDefs_DataDrivenLogin.i_login_with_credentials_and(StepDefs_DataDrivenLogin.java:23)       在?。我使用凭据“ tomsmith”和“ ***************”登录(资源/功能/DataDrivenLogin.feature:7)   创建人:org.openqa.selenium.InvalidSelectorException:无效的选择器:   无法使用xpath表达式定位元素   // frameset / frame / html / body / div([@ class ='tintedBox'和   @ id ='loginBox'] / table / tbody / tr / td / input [@ id ='UserID'和   name ='UserID']),原因是出现以下错误:SyntaxError:无法   在“文档”上执行“评估”:字符串   '// frameset / frame / html / body / div([@ class ='tintedBox'和   @ id ='loginBox'] / table / tbody / tr / td / input [@ id ='UserID'和   name ='UserID'])'不是有效的XPath表达式。 (会话信息:   chrome = 73.0.3683.103)(驱动程序信息:chromedriver = 2.46.628402   (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform = Windows NT   6.3.9600 x86_64)(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:0毫秒   有关此错误的文档,请访问:   https://www.seleniumhq.org/exceptions/invalid_selector_exception.html   构建信息:版本:“ 3.141.59”,修订版:“ e82be7d358”,时间:   '2018-11-14T08:25:48'系统信息:主机:'AP01154W8N',IP:   '192.168.56.1',os.name:'Windows 8.1',os.arch:'amd64',os.version:   '6.3',java.version:'1.8.0_191'驱动程序信息:   org.openqa.selenium.chrome.ChromeDriver功能   {acceptInsecureCerts:否,acceptSslCerts:否,   applicationCacheEnabled:否,browserConnectionEnabled:否,   browserName:chrome,chrome:{chromedriver版本:2.46.628402   (536cd7adbad73a ...,userDataDir:C:\ Users \ sinham0 \ AppData \ Lo ...},   cssSelectorsEnabled:true,databaseEnabled:false,goog:chromeOptions:   {debuggerAddress:localhost:65139},handlesAlerts:true,   hasTouchScreen:false,javascriptEnabled:true,   locationContextEnabled:true,mobileEmulationEnabled:false,   nativeEvents:true,networkConnectionEnabled:false,pageLoadStrategy:   正常,平台:XP,平台名称:XP,代理:Proxy(),可旋转:   false,setWindowRect:true,strictFileInteractability:false,   takesHeapSnapshot:true,takesScreenshot:true,超时:{隐式:   0,pageLoad:300000,脚本:30000},unantAlertBehaviour:忽略,   unhandledPromptBehavior:忽略,版本:73.0.3683.103,   webStorageEnabled:true}会话ID:869efd2308887ee98e8d561566b5a606   ***元素信息:{使用= xpath,值= // frameset / frame / html / body / div([@ class ='tintedBox'和   @ id ='loginBox'] / table / tbody / tr / td / input [@ id ='UserID'和   name ='UserID'])}       在sun.reflect.GeneratedConstructorAccessor10.newInstance(未知来源)上

1 个答案:

答案 0 :(得分:0)

得到TimeoutException的原因是要找到的元素在一个框架中,因此您必须切换到该框架。否则,webdriver无法找到该元素。

首先,找到frame标签的适当选择器。根据您提供的HTML,我正在构造定位器。

XPath:

WebElement loginFrame = driver.findElement(By.xpath("//frameset/frame[@src='Script/Login.asp?']"));

CSS选择器:

WebElement loginFrame = driver.findElement(By.cssSelector("frameset > frame[src='frame_c.htm']"));

尝试一下:



@Test
public void enterApplicationCerdentials(String userName, String password) {
WebDriverWait wait = new WebDriverWait(driver, 50);
By frameLocator =  By.xpath("//frameset/frame[@src='Script/Login.asp?']");

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//frameset/frame[@src='Script/Login.asp?']")));
driver.switchTo().frame(driver.findElement(frameLocator));

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("UserName"))).sendKeys("UserName");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Password"))).sendKeys("Password");
captureScreen();
}