gpt4 book ai didi

java - 通过 HTML 中的文本选择 WebElement

转载 作者:行者123 更新时间:2023-11-30 03:13:50 26 4
gpt4 key购买 nike

我尝试选择此 WebElement 一段时间了。这是我测试的 Web 应用程序首页上的注册按钮。我想知道是否有一种方法可以用来抓取 HTML 中的文本来定义 WebElement?

<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&nbsp;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&nbsp;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。

最佳答案

This response will not help if you are bound to only xpath or css lookups.

选项 1

如果您愿意在辅助方法中添加另一个案例,我认为 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

我的最后建议是尝试实现您自己的查找。我无法建议查找的实现,但如果您需要这种行为,那么您应该意识到您可能会创建一个子类来执行您想要的操作。

祝你好运。

关于java - 通过 HTML 中的文本选择 WebElement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33086092/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com