有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法从html站点代码导入数字

所以我想用下面的代码从网站上获取号码。我想要的是输入号码“Pieniądze”(21762)并替换“”和“PLN”,所以它将是号码,但我下面发布的代码不起作用。有什么建议吗,解析的错误在哪里

 int money;
 money=Integer.parseInt((driver.findElement(By.xpath("/html/body/div[1]/div/div[5]/div[4]/table/tbody/tr/td[2]/text()[1]"))).getText().replace(" ", "").replace("PLN", ""));

Html of what I want

run: Starting ChromeDriver 2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129) on port 24400 Only local connections are allowed. Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: The result of the xpath expression "/html/body/div1/div/div[5]/div[4]/table/tbody/tr/td[2]/text()1" is: [object Text]. It should be an element. (Session info: chrome=53.0.2785.89) (Driver info: chromedriver=2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129),platform=Windows NT 6.3.9600 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 23 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html Build info: version: 'unknown', revision: '2aa21c1', time: '2016-08-02 14:59:43 -0700' System info: host: 'Myszsoda', ip: '192.168.1.15', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_101' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129), userDataDir=C:\Users\Soda\AppData\Local\Temp\scoped_dir6316_26092}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=53.0.2785.89, platform=WIN8_1, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}] Session ID: 1627abc13cd518cda22e084f22304b80 *** Element info: {Using=xpath, value=/html/body/div1/div/div[5]/div[4]/table/tbody/tr/td[2]/text()1} 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:683) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:377) at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:506) at org.openqa.selenium.By$ByXPath.findElement(By.java:361) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:369) at dunnoyet.DunnoYet.main(DunnoYet.java:132) C:\Users\Soda\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 20 seconds)


共 (1) 个答案

  1. # 1 楼答案

    你的xpath返回String,而不是WebElement。从xpath中删除/text()[1]或从结果中删除getText()方法

    driver.findElement(By.xpath("...")).replace(" ", "")...