org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素

时间:2017-01-04 01:05:05

标签: java html css selenium

我很难获得正确的xpath或通过Selenium找到这个元素。得到此错误:

失败:homeLoan(“John Smith”,“john.smith @ test.com”,“Australia”,“1563”,“62365896410”,“Home Loan”,“Buying”,“100000”,“50000” ) org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“method”:“name”,“selector”:“FullName”}

我的硒代码是:

@Test(dataProvider="homeLoan")
public void homeLoan(String fullname, String email, String countryTypes, String postcode, String phone, String loanType, String loanPurposes, String securityValue, String homeLoanAmount) throws Exception {
    // open | https://test-www.loans.com.au/
    driver.get("https://test-www.loans.com.au/");
    // click | link=contact | 
    driver.findElement(By.linkText("contact")).click();
    // type | name=FullName
    **driver.findElement(By.name("FullName")).clear();
    driver.findElement(By.name("FullName")).sendKeys(fullname);**
    // type | name=Email
    driver.findElement(By.name("Email")).clear();
    driver.findElement(By.name("Email")).sendKeys(email);

这是html,我认为我想要查找的元素是在iFrame中:

<body style="overflow:hidden;" onload="iframe_resize();">
<div class="UWP">
<link id="cssLink" type="text/css" rel="stylesheet" data-bind="attr: { href: customStylesheetLocation}" href="/CMSPages/GetResource.ashx?stylesheetname=Referrer_Loans"/>
<!--<div data-bind="template: { name: 'loading' }, visible: IsLoading()"></div>-->
<input id="SessionLandingPageOriginalUrl" name="SessionLandingPageOriginalUrl" value="https://test-www.loans.com.au:443/uwp/callme/?sl=loans&typeid=5&action=hs&f=UWPIFrameCallSingleColumn" type="hidden"/>
<input id="SessionID" name="SessionID" value="yfuywfsdebpmcv0j3i1peylw" type="hidden"/>
<input id="SessionHostServer" name="SessionHostServer" value="TEST-K9-A1" type="hidden"/>
<input id="SessionReferrerUrl" name="SessionReferrerUrl" value="https://test-www.loans.com.au/" type="hidden"/>
<input id="ClientIPAddress" name="ClientIPAddress" value="172.21.0.200" type="hidden"/>
<input id="ClientUserAgent" name="ClientUserAgent" value="Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0" type="hidden"/>
<input id="ClientJavascriptEnabled" data-val="true" data-val-required="The ClientJavascriptEnabled field is required." name="ClientJavascriptEnabled" value="False" type="hidden"/>
<input id="ClientCookiesEnabled" data-val="true" data-val-required="The ClientCookiesEnabled field is required." name="ClientCookiesEnabled" value="False" type="hidden"/>
<div id="container" data-bind="visible: !IsLoading()" style="">
<div class="col-sm-12">
<div class="form-group">
<div data-bind="template: { name: 'commoncontrol', data: CommonControl }">
<div data-bind="visible:ShowCustomerDetails">
<div data-bind="visible:ShowNewCustomerButtons">
<div class="control col-sm-12 valid">
<div class="form-group">
**<input class="form-control inputbox bg-icon-name" placeholder="Full Name*" data-bind="attr: { name: FieldPrefix() + 'FullName' }, value: FullName, enable: ShowCustomerDetails" required="required" name="FullName" type="text"/>**
<div class="field-error">This field is required.</div>
</div>
</div>
<div class="control col-sm-12" style="margin-top:-15px;" data-bind="css: { invalid: ShowNameError() }">
<div class="control col-sm-12 valid">

粗体字的html代码是我正在寻找的xpath。

任何帮助都可以。提前谢谢。

1 个答案:

答案 0 :(得分:1)

您需要做的就是在尝试定位元素之前切换到框架,如下所示:

driver.switchTo().frame("UWPIFrame");

您的代码如下所示:

@Test(dataProvider="homeLoan")
public void homeLoan(String fullname, String email, String countryTypes, String postcode, String phone, String loanType, String loanPurposes, String securityValue, String homeLoanAmount) throws Exception {
    // open | https://test-www.loans.com.au/
    driver.get("https://test-www.loans.com.au/");
    // click | link=contact | 
    driver.findElement(By.linkText("contact")).click();
    //Switch to the frame 
    driver.switchTo().frame("UWPIFrame");
    // type | name=FullName
    driver.findElement(By.name("FullName")).clear();
    driver.findElement(By.name("FullName")).sendKeys(fullname);
    // type | name=Email
    driver.findElement(By.name("Email")).clear();
    driver.findElement(By.name("Email")).sendKeys(email);

但请记住,如果您尝试找到另一个不在iframe中的元素,则需要切换回默认内容,如下所示:

driver.switchTo().defaultContent();
相关问题