我的任务是为Google幻灯片网站创建一些自动化测试。我要访问的页面元素之一是左侧面板中的幻灯片选择。除其他外,我需要测试幻灯片删除功能,因此我需要能够选择特定的幻灯片。我首先尝试单击最初创建的一个。这就是我要初始化它的方式。
@FindBy(xpath = "//*[@id=\"filmstrip\"]/div/svg")
private lateinit var slideField: WebElement
此处提供的xpath是直接从浏览器复制的,对于许多其他元素,相同的方法也可以很好地工作。而且,每次单击此元素时,我的测试函数都会崩溃。
fun testClick(){
val wait = WebDriverWait(driver, 10)
wait.until<WebElement>(ExpectedConditions.elementToBeClickable(fileMenu))
fileMenu.click()
slideImportMenuItem.click()
pressEscKey(driver)
fileMenu.click()
createCopyMenuItem.click()
pressEscKey(driver)
slideField.click()
editMenu.click()
copyElementButton.click()
editMenu.click()
undoButton.click()
editMenu.click()
redoButton.click()
editMenu.click()
undoButton.click()
}
请注意,由于单击幻灯片与功能执行的第一个动作相距甚远,因此与我设法找到的许多类似问题不同,问题不在于未及时初始化元素。该元素似乎也没有出现在任何单独的框架中,这会阻止驾驶员对其进行检测。仍然出现此错误消息:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="filmstrip"]/div/svg/g"}
(Session info: chrome=75.0.3770.80)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
System info: host: 'WINDOWS-187BSSK', ip: '172.23.40.225', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_201'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 75.0.3770.80, chrome: {chromedriverVersion: 75.0.3770.8 (681f24ea911fe7..., userDataDir: C:\Users\Vlad\AppData\Local...}, goog:chromeOptions: {debuggerAddress: localhost:61884}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 19d219c567112fab9a909281c2a3fed9
*** Element info: {Using=xpath, value=//*[@id="filmstrip"]/div/svg/g}
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.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy4.click(Unknown Source)
at pages.MainPage.testClick(MainPage.kt:84)
at MainKt.main(main.kt:17)
at MainKt.main(main.kt)
我对Selenium和测试工作还是很陌生的,因此可以提供任何帮助。
编辑:我尝试创建另一张幻灯片,然后使用
@FindBy(xpath = "//*[@id=\"filmstrip\"]/div/svg[1]")
private lateinit var slideField: WebElement
和
@FindBy(xpath = "//*[@id=\"filmstrip\"]/div/svg[2]")
private lateinit var slideField: WebElement
分别,没有效果。