2017-03-09 57 views
0

我遇到了一个问题,我点击网页中的一个元素,然后创建一段代码,允许您作为用户点击并输入数量。但是,当我试图用Selenium自动化这个时,我无法访问这些新元素。如果我只是做:在Java中使用Selenium点击由Javascript生成的字段

driver.findElements(By.cssSelector("id^=field")); 

它抛出该异常:

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Cannot set property 'name' of undefined 

如果我看pageSource点击元素后新生成的html在那里,我可以看到它的ID就是我想要的需要搜索。任何帮助将非常感激。

编辑:

代码用于查找并单击该元素,其上激活的javascript:

List<WebElement> buttons = element.findElements(By.cssSelector("[id^=fieldButtons]")); 
for (WebElement element : buttons) { 
    element.click(); 
    // Here is where I want to then access the field and enter data. 
} 

下面是当使用完整的堆栈跟踪:

new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("id^=field"))); 


Starting ChromeDriver 2.27.440175 (9bc1d90b8bfa4dd181fbbf769a5eb5e575574320) on port 18209 
Only local connections are allowed. 
Mar 10, 2017 7:13:26 AM org.openqa.selenium.support.ui.ExpectedConditions findElement 
WARNING: WebDriverException thrown by findElement(By.cssSelector: id^=field) 
org.openqa.selenium.WebDriverException: unknown error: Cannot set property 'name' of undefined 
(Session info: chrome=56.0.2924.87) 
(Driver info: chromedriver=2.27.440175 (9bc1d90b8bfa4dd181fbbf769a5eb5e575574320),platform=Linux 4.4.8-040408-generic x86_64) (WARNING: The server did not provide any stacktrace information) 
Command duration or timeout: 35 milliseconds 
Driver info: org.openqa.selenium.chrome.ChromeDriver 
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.27.440175 (9bc1d90b8bfa4dd181fbbf769a5eb5e575574320), userDataDir=/tmp/.org.chromium.Chromium.89aRVC}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=56.0.2924.87, platform=LINUX, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}] 
Session ID: 75f1b0b4a60c26c1d9ec0bac0e4fa0e4 
*** Element info: {Using=css selector, value=id^=field} 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206) 
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158) 
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678) 
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363) 
at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:492) 
at org.openqa.selenium.By$ByCssSelector.findElement(By.java:430) 
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355) 
at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:899) 
at org.openqa.selenium.support.ui.ExpectedConditions.access$000(ExpectedConditions.java:41) 
at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:205) 
at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:201) 
at org.openqa.selenium.support.ui.ExpectedConditions$22.apply(ExpectedConditions.java:653) 
at org.openqa.selenium.support.ui.ExpectedConditions$22.apply(ExpectedConditions.java:646) 
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:238) 
at webScrape.findFieldToFill(webScrape.java:215) 
at webScrape.<init>(webScrape.java:26) 
at Main.main(Main.java:6) 
+0

我已经添加的代码输入值用于查找元素,我一直在使用像其他地方的cssSelector,它似乎工作正常。 –

+1

您是否试图在激活javascript和查找元素之间添加5秒钟的隐式等待?我通常使用WebDriverWait设置元素查找,但我没有看到你的元素。 – mutt

+0

是的,我已经尝试过,我认为它可以找到它,因为我有一个不同的例外,因为它无法找到页面上的元素。这看起来不同,就像我需要初始化一些东西。我看了一下通过Selenium执行Javascript,但无法使其工作。 –

回答

0

您是否尝试过使用JavascriptExecutor输入金额并执行用户点击。对我来说,它可以帮助时,我无法成功点击或使用driver.click()driver.sendKeys()

例如像这样

List<WebElement> buttons = element.findElements(By.cssSelector("[id^=fieldButtons]")); 
    for (WebElement element : buttons) { 
     element.click(); 
     Thread.sleep(2000) //sometimes I use thread sleep for simple wait condition 
     ((JavascriptExecutor) driver).executeScript("document.getElementById(\"field\").setAttribute('amount','" + amount + "');"); 
     ((JavascriptExecutor) driver).executeScript("document.getElementById(\"button\").click();"); 
    }