我一直在尝试选择这个web元素一段时间。它是我测试的web应用程序首页上的一个注册按钮。我想知道,是否有一种方法,我可以用它来抓取HTML中的文本来定义一个web元素?
<div>
<div>
<div style="">
<div style="transform: translate(0px, 0px) scale(0.5, 0.5) rotate(0deg);">
<div role="img">
<img src="images/lsloginform/newskin/try_it_normal.png?1444658058414">
</div>
</div>
<div style="transform: translate(0px, 75px) scale(1, 1) rotate(0deg);">
<canvas height="7" width="75" style="top: 0px; left: 0px; width: 75px; height: 7px;"></canvas>
</div>
<div style="transform: translate(0px, 82px) scale(1, 1) rotate(0deg);">
<div style="font-family: franklin-gothic-ext-comp-urw; font-weight: 700; font-style: normal; font-size: 15px; opacity: 1; color: rgb(63, 63, 63); transform: translate(7px, 0px) scale(1, 1) rotate(0deg);">SIGN UP</div>
</div>
</div>
<div style="display: none;"></div>
<div style="display: none;">
<div style="transform: translate(0px, 0px) scale(0.5, 0.5) rotate(0deg);">
<div role="img">
<img src="images/lsloginform/newskin/try_it_MO.png?1444665385167">
</div>
</div>
<div style="transform: translate(0px, 75px) scale(1, 1) rotate(0deg);">
<canvas height="7" width="75" style="top: 0px; left: 0px; width: 75px; height: 7px;"></canvas>
</div>
<div style="transform: translate(0px, 82px) scale(1, 1) rotate(0deg);">
<div style="font-family: franklin-gothic-ext-comp-urw; font-weight: 700; font-style: normal; font-size: 15px; opacity: 1; color: rgb(63, 63, 63); transform: translate(7px, 0px) scale(1, 1) rotate(0deg);">SIGN UP</div>
</div>
<div></div>
</div>
<div style="display: none;"></div>
<div style="display: none;"></div>
</div>
</div>
具体的例子,我想在HTML的末尾使用"SIGN UP"文本来使用上面div的xpath。我该怎么做呢?
我测试的应用程序非常挑剔,能够根据该属性进行选择将使我的工作轻松得多。这是一个问题,因为我需要3种不同的方法来抓取xpath:一种用于正常的按钮,一种用于悬停,另一种用于按钮在单击时瞬间发生变化。我的大多数自动化脚本看起来像这样:
public class changeLanguageLogin {
public void run(WebDriver driver) {
WebElement changeLanguage = getWebElement(driver, "xpath", "img", "src", "select_application_language_normal");
Actions actions = new Actions(driver);
actions.moveToElement(changeLanguage).build().perform();
WebElement changeLanguageMO = getWebElement(driver, "xpath", "img", "src", "select_application_language_hover");
changeLanguageMO.click();
WebElement changeLanguageClicked = getWebElement(driver, "xpath", "img", "src", "select_application_language_pushed");
changeLanguageClicked.click();
}
private static By buildXPathExpression(String htmlElement, String attributeName, String attributeValue)
{
return By.xpath("//"+htmlElement+"[contains(@"+attributeName+",'"+attributeValue+"')]");
}
private static By buildCSSExpression(String htmlElement, String attributeName, String attributeValue)
{
return By.cssSelector(""+htmlElement+"["+attributeName+"='"+attributeValue+"']");
}
private static WebElement getWebElement(WebDriver driver, String operation, String htmlElement, String attributeName, String attributeValue)
{
By byObj = null;
WebElement webElementObj = null;
if(operation!= null)
{
if(operation.equals("xpath"))
{
byObj = buildXPathExpression(htmlElement,attributeName,attributeValue);
}
else if(operation.equals("cssSelector"))
{
byObj = buildCSSExpression(htmlElement,attributeName,attributeValue);
}
}
if(byObj != null)
{
webElementObj = driver.findElement(byObj);
}
return webElementObj;
}
}
一天中有很多很多次,一种定义xpath的方法"行不通",我不得不四处寻找另一种方法。我们的目标是不使用任何"硬编码"的xpath,因为这在我刚开始工作时是有问题的。
所以我正在寻找的答案是:通过HTML中的文本定义xpath, "SIGN UP"。
如果只绑定到xpath或css 查找。
选项1
如果你愿意在你的helper方法中添加另一个case,我认为By。linkText可以做你想要的?
/**
* @param linkText The exact text to match against
* @return a By which locates A elements by the exact text it displays
*/
public static By linkText(final String linkText) {
if (linkText == null)
throw new IllegalArgumentException(
"Cannot find elements when link text is null.");
return new ByLinkText(linkText);
}
或By.partialLinkText
/**
* @param linkText The text to match against
* @return a By which locates A elements that contain the given link text
*/
public static By partialLinkText(final String linkText) {
if (linkText == null)
throw new IllegalArgumentException(
"Cannot find elements when link text is null.");
return new ByPartialLinkText(linkText);
}
如果您对一个已经国际化的系统进行测试,这可能是一个潜在的问题。根据环境的不同,用户/浏览器的语言环境会改变你正在寻找的文本。
如果文本发生变化,这也可能是一个维护问题。
选项2
如果你可以控制HTML,你可以考虑给页面上的元素添加id。然后你可以使用By。id 查找。从我的一些初步研究来看,元素id被视为更可靠和一致的路径之一。
/**
* @param id The value of the "id" attribute to search for
* @return a By which locates elements by the value of the "id" attribute.
*/
public static By id(final String id) {
if (id == null)
throw new IllegalArgumentException(
"Cannot find elements with a null id attribute.");
return new ById(id);
}
选项3
我最后的建议是尝试实现你自己的By查找。我不会建议实现查找,但如果这种行为是你需要的,那么你应该意识到,你可能会创建一个子类来做你想要的。祝你好运
对于旧的inlie样式css,您生成css选择器取决于为div编写的样式,如"div[style*='paste_style_attribute']"
。但是,由于存在相同样式的重复,这种方法不可靠。
对于以下div:
<div style="font-family: franklin-gothic-ext-comp-urw; font-weight: 700; font-style: normal; font-size: 15px; opacity: 1; color: rgb(63, 63, 63); transform: translate(7px, 0px) scale(1, 1) rotate(0deg);">SIGN UP</div>
你可以生成以下css选择器:
"div[style*='font-family: franklin-gothic-ext-comp-urw; font-weight: 700; font-style: normal; font-size: 15px; opacity: 1; color: rgb(63, 63, 63); transform: translate(7px, 0px) scale(1, 1) rotate(0deg);']"
Java代码:driver.findElement(By.cssSelector("div[style*='font-family: franklin-gothic-ext-comp-urw; font-weight: 700; font-style: normal; font-size: 15px; opacity: 1; color: rgb(63, 63, 63); transform: translate(7px, 0px) scale(1, 1) rotate(0deg);']"))