簡體   English   中英

在Java中使用Selenium單擊Javascript生成的字段

[英]Use Selenium in Java to click on field generated by Javascript

我有一個問題,我單擊網頁中的一個元素,然后它創建一段代碼,使您作為用戶可以單擊並輸入金額。 但是,當我嘗試使用Selenium自動化此操作時,無法訪問這些新元素。 如果我只是做:

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)

您是否嘗試過使用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();"); 
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM