使用 webdriver 查找 webelement 的多个定位器
Multiple locator for finding webelement with webdriver
我在 QAF 中使用 Selenium Webdriver。
我面临的问题与在网页上查找元素有关。
对于少数元素,不同的定位器在不同的时间工作。
例如 - 有时 name=nameA 有效,有时 name=nameB (可能取决于 AUT 的不同环境,我不知道)。
查找下面的代码:
public class HomePage extends WebDriverBaseTestPage<WebDriverTestPage> {
@FindBy(locator="name=nameA")
private QAFWebElement btnSomeElement;
@Override
protected void openPage(PageLocator locator, Object... args) {
driver.get("/");
}
}
我应该怎么做才能解决这个问题?
你应该得到一个匹配两种环境的选择器,使用 css 或 xpath 选择器。
如果您需要任何帮助,请添加带有 sections/elements 的 html 片段。
假设您按名称进行选择并且此名称发生变化,您可以编写一个匹配两个名称的选择器,例如:
css: [name=nameA], [name=nameB]
xpath: //*[@name='nameA' or @name='nameB']
定位器有时会发生变化是很常见的。有些甚至是动态的,尤其是 table 行中的元素。最好使用更具体的定位器,如 xpath 来定位元素。
@FindBy(locator = "xpath=//button[text()='ButtonName']")
private QAFWebElement btnSomeElement;
虽然您已经在使用 QAF,但您已经拥有适用于此类用例的解决方案。首先你应该使用定位器存储库,
不要在页面中硬编码定位器,只需提供定位器键即可。
例如:
In page.loc File
my.ele.locator=<locatorStretegy>=<locator>
my.ele.locator=name=elemName
In Page class:
@FindBy(locator = "my.ele.loc")
private QAFWebElement btnSomeElement; Now coming to your problem, if most of the locator very with environment then you can utilize
resource management capabilities of QAF. In other case you can
use alternate locator strategy provided by QAF. For example:
my.ele.locator=['css=.cls#eleid','name=eleName','name=eleName2']
my.ele.locator=['name=eleNameEnv1','name=eleNameEnv2']
btn.login.page.logon.button = {\"locator\":[\"xpath=//div[contains(@id,'logonbutton')]//span[contains(text(),'Logon')]\",\"xpath=//*[contains(text(),'Logon') or @id='__screen0-logonBtn']\"]}
这是在 QAF 中使用多个定位器的纯文本文件的相同示例。适合我。
我在 QAF 中使用 Selenium Webdriver。 我面临的问题与在网页上查找元素有关。 对于少数元素,不同的定位器在不同的时间工作。
例如 - 有时 name=nameA 有效,有时 name=nameB (可能取决于 AUT 的不同环境,我不知道)。
查找下面的代码:
public class HomePage extends WebDriverBaseTestPage<WebDriverTestPage> {
@FindBy(locator="name=nameA")
private QAFWebElement btnSomeElement;
@Override
protected void openPage(PageLocator locator, Object... args) {
driver.get("/");
}
}
我应该怎么做才能解决这个问题?
你应该得到一个匹配两种环境的选择器,使用 css 或 xpath 选择器。
如果您需要任何帮助,请添加带有 sections/elements 的 html 片段。
假设您按名称进行选择并且此名称发生变化,您可以编写一个匹配两个名称的选择器,例如:
css: [name=nameA], [name=nameB]
xpath: //*[@name='nameA' or @name='nameB']
定位器有时会发生变化是很常见的。有些甚至是动态的,尤其是 table 行中的元素。最好使用更具体的定位器,如 xpath 来定位元素。
@FindBy(locator = "xpath=//button[text()='ButtonName']")
private QAFWebElement btnSomeElement;
虽然您已经在使用 QAF,但您已经拥有适用于此类用例的解决方案。首先你应该使用定位器存储库, 不要在页面中硬编码定位器,只需提供定位器键即可。
例如:
In page.loc File
my.ele.locator=<locatorStretegy>=<locator> my.ele.locator=name=elemName
In Page class:
@FindBy(locator = "my.ele.loc") private QAFWebElement btnSomeElement; Now coming to your problem, if most of the locator very with environment then you can utilize
resource management capabilities of QAF. In other case you can use alternate locator strategy provided by QAF. For example:
my.ele.locator=['css=.cls#eleid','name=eleName','name=eleName2'] my.ele.locator=['name=eleNameEnv1','name=eleNameEnv2']
btn.login.page.logon.button = {\"locator\":[\"xpath=//div[contains(@id,'logonbutton')]//span[contains(text(),'Logon')]\",\"xpath=//*[contains(text(),'Logon') or @id='__screen0-logonBtn']\"]}
这是在 QAF 中使用多个定位器的纯文本文件的相同示例。适合我。