我遇到一个问题,我点击网页中的元素,然后创建一段代码,允许您作为用户点击并输入和金额。但是,当我尝试使用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)
答案 0 :(得分: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();");
}