org.openqa.selenium.nosuchelementexception:没有这样的元素:无法定位元素

ddrv8njm  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(380)

我很难通过selenium获得正确的xpath或定位此元素。获取此错误:
失败:家庭贷款(“约翰史密斯”,“约翰。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。
任何帮助都可以。提前谢谢。

zsohkypk

zsohkypk1#

在尝试定位元素之前,只需切换到框架,如下所示:

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();

相关问题