2017-03-09 19 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();"); 
    } 
相關問題