简体   繁体   English

selenium webdriver无法找到div中隐藏的元素

[英]selenium webdriver unable to locate element hidden in div

I am using the selenium firefox webdriver in my java program. 我在Java程序中使用了selenium firefox webdriver。 I am trying to type a string into a textbox and click submit. 我正在尝试在文本框中键入一个字符串,然后单击“提交”。 However, the textbox is nested in a hidden element so I can't access it using driver.findElement with id, xpath or css. 但是,文本框嵌套在隐藏元素中,因此我无法使用带有id,xpath或css的driver.findElement访问它。 (It is visible on the webpage, however). (但是在网页上可以看到)。 I get the NoSuchElementException 我得到了NoSuchElementException

Here is the html of the webpage: 这是网页的html:

<div id="mainContent" style="overflow:hidden;">
<!-- content left here -->
<div id="contentLeft">
<script type="text/javascript" src="/enterprisesolutions/staticcontent/includes/scripts/QuickTask.js"/>
<script type="text/javascript" src="/enterprisesolutions/Includes/Javascript/date-picker.js"/>
<div id="quickTask">
<h2 class="boxContentHead">
<div id="qtBoxContent" class="boxContent quickTaskBack">
<!-- Accounts and Maintenance -->
<div id="qt_accounts_and_maintenance" class="qtStack" style="display:block">
<!-- Orders -->
<div id="qt_orders" class="qtStack" style="display:block">
<!-- Invoice -->
<div id="qt_invoices" class="qtStack" style="display:block">
<div class="qtIcon invoices"/>
<div class="qtContent">
<div class="qtTitle">Invoices</div>
<div class="qtForm">
<div class="qtLabel qtLabelInvoices">
<div class="qtLeftInvoices">
<div class="firstField">
<div class="qtFormLabel">Account/Phone/Circuit</div>
<div>
<input id="qt_invoices_text1" type="text" value=""/>
</div>
</div>
<div class="secondField">
</div>
<div class="qtRight">
</div>
</div>
</div>
<!-- Repairs -->
<div id="qt_repairs" class="qtStack" style="display:block">
<!-- Lottery Reporting -->
<!-- Reporting -->
<form id="quickTaskForm" name="quickTaskForm" action="/enterprisesolutions/global/quickTaskAction.do" method="post">
<!-- QT No Access -->
<!-- QT Request Access -->
<div id="qt_request_access" class="qtStackAccess" style="display:block">
</div>
<div id="qtBottom" class="quickTaskBack" style="background: none repeat scroll 0% 0% transparent;">
</div>
<!-- start promo and customized pod -->
<div id="promo_msg">
<!-- end promo and customized pod -->
</div>
<!-- end content left-->
<div id="contentRight" style="height: 845px;">
<!-- end content right -->
</div>

The div with id = mainContent has style of "overflow:hidden" which im guessing is causing the webdriver not find my element. id = mainContent的div具有“overflow:hidden”的样式,我猜测它会导致webdriver找不到我的元素。

I have tried the following but they don't work since the field is hidden driver.findElement(By.cssSelector("#qt_invoices_text1")).sendKeys("hi"); 我尝试了以下但是它们不起作用,因为该字段是隐藏的driver.findElement(By.cssSelector("#qt_invoices_text1")).sendKeys("hi"); driver.findElement(By.id("qt_invoices_text1")).sendKeys("hi"); (and with the correct xpath.) (以及正确的xpath。)

I have heard there is a way around this using JavascriptExecutor but I haven't found one yet. 我听说可以使用JavascriptExecutor解决此问题,但是我还没有找到。

EDIT: Here is the stacktrace: 编辑:这是堆栈跟踪:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"#qt_invoices_text1"}
Command duration or timeout: 20.18 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.33.0', revision: '4e90c97', time: '2013-05-22 15:33:32'
System info: os.name: 'Windows 8', os.arch: 'amd64', os.version: '6.2', java.version: '1.7.0_21'
Session ID: 334122ae-6eab-4036-96ca-1183c9129b51
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=21.0}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:191)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:396)
    at org.openqa.selenium.By$ByCssSelector.findElement(By.java:407)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)
    at DownloadInvoice.read(DownloadInvoice.java:282)
    at DownloadInvoice.main(DownloadInvoice.java:406)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"css selector","selector":"#qt_invoices_text1"}
Build info: version: '2.33.0', revision: '4e90c97', time: '2013-05-22 15:33:32'
System info: os.name: 'Windows 8', os.arch: 'amd64', os.version: '6.2', java.version: '1.7.0_21'
Driver info: driver.version: unknown

And here is my java code (irrelevant kind of but people kept asking) (part of a bigger project this is the only relevant code): 这是我的java代码(无关紧要,但人们不断询问)(一个更大的项目的一部分,这是唯一相关的代码):

driver.get(sheet.getCell(j+1,i).getContents());
                driver.findElement(By.id("UserId")).click();
                driver.findElement(By.id("UserId")).clear();
                driver.findElement(By.id("UserId")).sendKeys(sheet.getCell(j+2,i).getContents());
                Thread.sleep(1000);
                driver.findElement(By.id("PASSWORD")).click();
                driver.findElement(By.id("PASSWORD")).clear();                                
                driver.findElement(By.id("PASSWORD")).sendKeys(sheet.getCell(j+3,i).getContents());
                Thread.sleep(5000);
                driver.findElement(By.id("submit")).click();
                Thread.sleep(2000);
            driver.findElement(By.cssSelector("#qt_invoices_text1")).sendKeys("hi");
driver.findElement(By.id("submit")).click();

EDIT: I played around with Selenium IDE and it selects the frame before typing with the command SelectFrame _shellbody but when I export it shows this as a comment in the code: // ERROR: Caught exception [ERROR: Unsupported command [selectFrame | _shellbody | ]] 编辑:我玩过Selenium IDE,并在使用命令SelectFrame _shellbody输入之前选择了框架,但是当我导出时,它在代码中显示为注释: // ERROR: Caught exception [ERROR: Unsupported command [selectFrame | _shellbody | ]] // ERROR: Caught exception [ERROR: Unsupported command [selectFrame | _shellbody | ]]

Any help is appreciated! 任何帮助表示赞赏!

Try use WebDriverWait , it makes no sense talking about visibility now, because the exception is NoSuchElementException , which means the element is not there. 尝试使用WebDriverWait ,现在谈论可见性是没有意义的,因为异常是NoSuchElementException ,这意味着该元素不存在。 After resolve this exception, then we can move through talking about visibility. 解决此异常后,我们可以继续讨论可见性。

driver.switchTo().frame(0); // use this if it's in frame and you don't know how to locate it
WebElement input = (new WebDriverWait(driver, 15)).until(ExpectedConditions.presenceOfElementLocated(By.id("qt_invoices_text1")));

Also just a heads up, don't use Thread.Sleep() , use WebDriverWait please. 也只是抬头,不要使用Thread.Sleep() ,请使用WebDriverWait

Even after sufficient wait, if it doesn't work then try JSExecutor. 即使经过足够的等待,如果仍然无法运行,请尝试JSExecutor。

JavascriptExecutor js = (JavascriptExecutor)driver; JavascriptExecutor js =(JavascriptExecutor)驱动程序;

js.executeScript("document.getElementById(div element id).click()"); js.executeScript(“ document.getElementById(div元素ID).click()”);

This should work..... 这应该工作.....

Need some wait, while executing the script your application takes some time to load the hidden element meanwhile selenium goes one step ahead & try to find the element but element not found because this element not yet loaded so, it check once and thrown an exception. 需要一些等待,在执行脚本时,您的应用程序需要一些时间来加载隐藏的元素,而selenium向前走了一步,并尝试查找该元素但未找到该元素,因为尚未加载此元素,因此它检查一次并引发异常。 use WebDriverWait take advantage over Thread.Sleep() . 使用WebDriverWait优先Thread.Sleep()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM