- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试选择此 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 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。
最佳答案
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/
ArrayList corousalItems= new ArrayList<>(); List listText= driver.findElements(By.cssSelector("Cor
我正在学习 Selenium,我有一个问题,假设我们有以下内容: 我可以使用带有 的 Css 选择器来选择包装元素列表.wrapper .所以,假设我有这些元素,我该如何选
我正在使用 Selenium WebDriver 和 Java。我正在尝试创建一个循环访问 WebElements 列表的方法,以返回包含文本“Ooga Booga”的第一个 WebElement,或
我正在尝试使用 Selenium 获取另一个提取元素内的元素。但我得到了意想不到的结果。例如,下面的代码在循环中返回相同的“数据时间”和推文时间。请注意“data-item-id”如何不同,但“dat
对源代码进行了简短的扫描,不确定最佳做法是什么。 例如,假设我有一个页面对象“DummyPage”,它有两个面板元素TopPanel 和BottomPanel。每个面板都有一些元素,这些元素由 Top
List fields = (List) driver.findElement(By.xpath("//input[@type='text']")); System.out.println(field
我想在 List 中找到 WebElement通过文本。我的方法有这样的参数:List webElements, String text .对于匹配文本,我更喜欢使用 javaslang图书馆。所以,
如何比较两个 selenium WebElements 看它们是否相同? 首先,我检索了 input_fields 和 first_input 元素的列表: self.input_fields = d
我正在使用 Appium,我想打印列表中元素的名称。我正在使用以下代码 List list = getDriver().findElementsByXPath(getLocator(Locators.
任何人都可以帮助我了解如何使用这两个函数来获取任何 CSS 属性的值。 最佳答案 如果有特定的标签如下 driver.getElement(By.tagName("img")).getAttribu
我正在从 facebook 获取日期网络元素,并使用下面的代码循环它。 public class select_facebook { public static void main(Strin
所以我尝试使用 XPath 从一个 webelement 到另一个 webelement。这就是 webelement 所在的路径,我通过使用“findElementsByClassName”找到它。
我想在 YouTube 评论页面上按此顺序单击这三个不同的网络元素 showmorebutton、viewrepliesbutton、readmorebutton(例如 https://www.you
如何在webelement列表中存储webelement?我已经为 webelement 创建列表并在 add 方法中传递 webelement obj,但系统显示空异常。Java代码:List el
我已经弄清楚如何使用这篇 Stack overflow 帖子在 python 中使用 Selenium 查找元素的子元素。 Selenium Get WebElement inside a WebEl
我创建了这个测试脚本来测试网站。问题是,有时我可以单击编辑按钮,但有时我会收到同一按钮的 NoElementFoundException 异常。我在论坛上搜索了相同类型的问题,并按照建议更改了代码,但
我有个问题。 是什么让 FirefoxDriver 能够找到 WebElements 并在 java 代码中单击它们,但是当使用 HtmlUnitDriver 运行相同的代码时,找不到相同的 WebE
我编写了一个java类(CustomerHistoryMapping),其中包含一个HashMap(String,WebElement),如下面的代码(代码的第一部分)我的总体目标是能够使用给定的 W
我对 Selenium 测试还很陌生,所以如果我的问题听起来很简单,我提前道歉。 我正在使用 Java 和 Selenium 编写测试。有时我需要单击 WebElement(例如按钮)。如果我使用 e
Page page = new Page(); page.populateProductList( driver.findElement( By.xpath("//div[@id='my_25_pro
我是一名优秀的程序员,十分优秀!