select 使用 selenium webdriver 的按钮的问题
Problem to select a button using selenium webdriver
我正在尝试使用带有 java 的 seleninum webdriver select 特定按钮的不同方法,但不幸的是,没有任何效果。
当我使用 Selenium 进行测试时 IDE 可以正常工作。例如,我复制了相同的 xpath,但是当我尝试在我的 java 应用程序中进行测试时,没有任何效果。我尝试使用不同的方式,By.cssSelector 和 By.path。
这是我的 html:
<section class="fd-section"><fd-action-bar><div class="fd-action-bar"><fd-action-bar-header class="fd-action-bar__header"><fd-action-bar-title><h1 class="fd-action-bar__title"> Applications </h1></fd-action-bar-title></fd-action-bar-header><fd-action-bar-actions class="fd-action-bar__actions"><y-list-search _nghost-c4="" hidden=""><!----><!----><div _ngcontent-c4="" clickoutsideevents="click,mousedown" excludebeforeclick="true" class="ng-star-inserted"><!----><button _ngcontent-c4="" fd-button="" class="fd-button xyz-icon--search fd-button--light ng-star-inserted"></button><!----></div></y-list-search><y-list-filter _nghost-c5="" hidden=""><!----></y-list-filter><!----><button class="open-create-namespace-modal fd-button xyz-icon--add ng-star-inserted" fd-button=""> Create Application </button></fd-action-bar-actions></div></fd-action-bar></section>
我需要 select 带有文本“创建应用程序”的按钮。
当我使用 Selenium 创建测试时 IDE 此按钮的 xpath 是:
//按钮[包含(.,'创建应用程序')]
基本上,我的 java 代码是:
public WebElement wElement;
wElement = driver.findElement(By.xpath("//button[contains(.,' Create Application')]"));
wElement.click();
这是异常信息:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[contains(.,' Create Application')]"}
(Session info: chrome=76.0.3809.100)
(Driver info: chromedriver=72.0.3626.69 (3c16f8a135abc0d4da2dff33804db79b849a7c38),platform=Mac OS X 10.14.6 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:19:58.91Z'
System info: host: 'C02WW0BZHTD8', ip: 'fe80:0:0:0:8f6:17e1:1a28:1e23%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.14.6', java.version: '1.8.0_171'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 72.0.3626.69 (3c16f8a135abc..., userDataDir: /var/folders/2r/99nyn7t16cz...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:60374}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, proxy: Proxy(), rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unexpectedAlertBehaviour: ignore, unhandledPromptBehavior: ignore, version: 76.0.3809.100, webStorageEnabled: true}
Session ID: b2341899cd9b62b0169b02371aaa3018
*** Element info: {Using=xpath, value=//button[contains(.,' Create Application')]}
执行这段代码时,按钮是否已经载入页面?
{implicit: 0, pageLoad: 300000, script: 30000}
,表明驱动程序不会隐式等待找到任何元素。即如果该元素不可用,它将立即抛出异常。
尝试driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
,然后再尝试找到按钮。
也可以尝试使用以下定位器 -
//button[contains(.,'Create Application')]
//button[contains(text(),'Create Application')]
如果上面的none有效,请您提供一个URL(如果是public)
还要检查按钮是否在框架内。
此时可以使用JavascriptExecutor接口尝试点击按钮
WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Create Application')]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
也试试 WebDriverWait
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Create Application')]"))).click();
所需的元素是 Angular element so to locate the element you have to induce WebDriverWait for the elementToBeClickable()
and you can use either of the following :
cssSelector
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("fd-action-bar-actions.fd-action-bar__actions button.open-create-namespace-modal.fd-button.xyz-icon--add.ng-star-inserted"))).click();
xpath
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='open-create-namespace-modal fd-button xyz-icon--add ng-star-inserted' and contains(., 'Create Application')]"))).click();
其他注意事项
确保:
- JDK 升级到当前级别 JDK 8u212.
- Selenium 已升级到当前级别 Version 3.141.59。
- Chrome驱动程序 已更新至当前 ChromeDriver v76.0 级别。
- Chrome 已更新至当前 Chrome 版本 76.0 级别。 (根据 ChromeDriver v76.0 release notes)
Here you can find a detailed discussion on
我正在尝试使用带有 java 的 seleninum webdriver select 特定按钮的不同方法,但不幸的是,没有任何效果。
当我使用 Selenium 进行测试时 IDE 可以正常工作。例如,我复制了相同的 xpath,但是当我尝试在我的 java 应用程序中进行测试时,没有任何效果。我尝试使用不同的方式,By.cssSelector 和 By.path。
这是我的 html:
<section class="fd-section"><fd-action-bar><div class="fd-action-bar"><fd-action-bar-header class="fd-action-bar__header"><fd-action-bar-title><h1 class="fd-action-bar__title"> Applications </h1></fd-action-bar-title></fd-action-bar-header><fd-action-bar-actions class="fd-action-bar__actions"><y-list-search _nghost-c4="" hidden=""><!----><!----><div _ngcontent-c4="" clickoutsideevents="click,mousedown" excludebeforeclick="true" class="ng-star-inserted"><!----><button _ngcontent-c4="" fd-button="" class="fd-button xyz-icon--search fd-button--light ng-star-inserted"></button><!----></div></y-list-search><y-list-filter _nghost-c5="" hidden=""><!----></y-list-filter><!----><button class="open-create-namespace-modal fd-button xyz-icon--add ng-star-inserted" fd-button=""> Create Application </button></fd-action-bar-actions></div></fd-action-bar></section>
我需要 select 带有文本“创建应用程序”的按钮。
当我使用 Selenium 创建测试时 IDE 此按钮的 xpath 是:
//按钮[包含(.,'创建应用程序')]
基本上,我的 java 代码是:
public WebElement wElement; wElement = driver.findElement(By.xpath("//button[contains(.,' Create Application')]")); wElement.click();
这是异常信息:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[contains(.,' Create Application')]"} (Session info: chrome=76.0.3809.100) (Driver info: chromedriver=72.0.3626.69 (3c16f8a135abc0d4da2dff33804db79b849a7c38),platform=Mac OS X 10.14.6 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 0 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:19:58.91Z' System info: host: 'C02WW0BZHTD8', ip: 'fe80:0:0:0:8f6:17e1:1a28:1e23%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.14.6', java.version: '1.8.0_171' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 72.0.3626.69 (3c16f8a135abc..., userDataDir: /var/folders/2r/99nyn7t16cz...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:60374}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, proxy: Proxy(), rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unexpectedAlertBehaviour: ignore, unhandledPromptBehavior: ignore, version: 76.0.3809.100, webStorageEnabled: true} Session ID: b2341899cd9b62b0169b02371aaa3018 *** Element info: {Using=xpath, value=//button[contains(.,' Create Application')]}
执行这段代码时,按钮是否已经载入页面?
{implicit: 0, pageLoad: 300000, script: 30000}
,表明驱动程序不会隐式等待找到任何元素。即如果该元素不可用,它将立即抛出异常。
尝试driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
,然后再尝试找到按钮。
也可以尝试使用以下定位器 -
//button[contains(.,'Create Application')]
//button[contains(text(),'Create Application')]
如果上面的none有效,请您提供一个URL(如果是public)
还要检查按钮是否在框架内。
此时可以使用JavascriptExecutor接口尝试点击按钮
WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Create Application')]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
也试试 WebDriverWait
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Create Application')]"))).click();
所需的元素是 Angular element so to locate the element you have to induce WebDriverWait for the elementToBeClickable()
and you can use either of the following
cssSelector
:new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("fd-action-bar-actions.fd-action-bar__actions button.open-create-namespace-modal.fd-button.xyz-icon--add.ng-star-inserted"))).click();
xpath
:new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='open-create-namespace-modal fd-button xyz-icon--add ng-star-inserted' and contains(., 'Create Application')]"))).click();
其他注意事项
确保:
- JDK 升级到当前级别 JDK 8u212.
- Selenium 已升级到当前级别 Version 3.141.59。
- Chrome驱动程序 已更新至当前 ChromeDriver v76.0 级别。
- Chrome 已更新至当前 Chrome 版本 76.0 级别。 (根据 ChromeDriver v76.0 release notes)
Here you can find a detailed discussion on