提问人:ithi 提问时间:8/1/2023 最后编辑:The Coding Penguinithi 更新时间:8/1/2023 访问量:28
在html代码中搜索xpath,突出显示所需的确切路径,但在代码中它不起作用
Searching the xpath in html code, highlights the exact path which is need, but inside the code it is not working
问:
这是我在运行代码时遇到的异常:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//span[contains(.,'STEP3 Orientation')]"}
(Session info: chrome=115.0.5790.110)
For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Build info: version: '4.10.0', revision: 'c14d967899'
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '17.0.7'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [3b7c5c64e8bf5a509fa70a6b5f095c21, findElement {using=xpath, value=//span[contains(.,'STEP3 Orientation')]}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 115.0.5790.110, chrome: {chromedriverVersion: 114.0.5735.90 (386bc09e8f4f..., userDataDir: C:\Users\User\AppData\Local...}, goog:chromeOptions: {debuggerAddress: localhost:61749}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: windows, proxy: Proxy(), se:cdp: ws://localhost:61749/devtoo..., se:cdpVersion: 115.0.5790.110, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}
Session ID: 3b7c5c64e8bf5a509fa70a6b5f095c21
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:199)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:132)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:51)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:191)
at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:196)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:171)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:531)
at org.openqa.selenium.remote.ElementLocation$ElementFinder$2.findElement(ElementLocation.java:165)
at org.openqa.selenium.remote.ElementLocation.findElement(ElementLocation.java:59)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:350)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344)
at Selenium.Demo1.main(Demo1.java:30)
I am trying to fetch the details using the following xpath:
//driver.findElement(By.xpath("//span[contains(text(),'STEP3 Orientation')]")).getText();
//driver.findElement(By.xpath("//span[contains(@class,'pageTitle')]")).getText();
//driver.findElement(By.xpath("//span[contains(.,'STEP3 Orientation')]")).getText();
对于所有 3 个 xpath,我都得到了 NoSuchElementException
html 代码 在 html 代码中搜索 xpath,突出显示所需的确切路径 在 html 代码中搜索 xpath,突出显示所需的确切路径,但在代码中它不起作用。
答:
0赞
Shawn
8/1/2023
#1
尝试应用 selenium 的 Waits。以下代码用于查找所需的元素:ExplicitWait
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(30));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(text(),'STEP3 Orientation')]"))).getText();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(@class,'pageTitle')]"))).getText();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(.,'STEP3 Orientation')]"))).getText();
进口:
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
评论
0赞
ithi
8/2/2023
非常感谢。这有助于解决问题。
评论