- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我知道 WebElement 可点击的测试普遍情况:
用这样的方式测试它:
public static boolean isElementFoundDisplayedEnabled(WebDriver driver, String accessor){
return driver.findElements(By.xpath(accessor)).size() > 0 && driver.findElement(By.xpath(accessor)).isDisplayed() && driver.findElement(By.xpath(accessor)).isEnabled();
//isDisplayed(): method avoids the problem of having to parse an element's "style" attribute to check hidden/visible. False when element is not present
//isEnabled(): generally return true for everything but disabled input elements.
}
此函数有缺陷,它仅检查元素是否在 DOM 级别可点击,但如果由于某些 css 困惑,元素被隐藏/重叠,则可以得到异常:
org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (781, 704). Other element would receive the click:
...
在这种情况下,仍然可以使用以下方法单击该元素:
// Assume driver is a valid WebDriver instance that
// has been properly instantiated elsewhere.
WebElement element = driver.findElement(By.id("gbqfd"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
但是,我很想知道,我们如何在不点击 executor.executeScript
的情况下检查 WebElement 是否被其他元素隐藏/重叠并且完全可点击。
有人可以透露一些信息吗,我已经对这些进行了几个小时的研究,但一无所获。
最佳答案
我不太喜欢创建像这样处理所有点击等的函数,但如果我被迫编写一个函数,它看起来会像这样。里面的评论解释了发生了什么。
public static void clickElement(By locator) throws InterruptedException
{
try
{
// first we try the standard wait for element to be clickable and click it
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(locator)).click();
}
catch (TimeoutException e)
{
// element never becomes present
}
catch (WebDriverException e)
{
// click is blocked by another element, retry for 10 seconds
while (true)
{
Instant timeOut = Instant.now().plusSeconds(10);
try
{
driver.findElement(locator).click();
break;
}
catch (WebDriverException e2)
{
// ignore additional blocked click exceptions
}
if (Instant.now().isAfter(timeOut))
{
// element is still blocked after retries for 10 seconds, fallback to JSE click
((JavascriptExecutor) driver).executeScript("arguments[0].click();", driver.findElement(locator));
}
// slight pause between loops
Thread.sleep(100);
}
}
}
关于您的功能的一些反馈...您应该传递 By
实例而不是字符串,例如
By locator = By.id("myId");
这样您的函数就可以更加灵活,并且不会仅硬编码为 XPath。此外,您正在使用您的功能抓取页面 3 次。抓取页面一次,然后使用存储的元素进行可见和启用的检查,例如
public static boolean isElementFoundDisplayedEnabled(WebDriver driver, By locator)
{
List<WebElement> e = driver.findElements(locator);
return !e.isEmpty() && e.get(0).isDisplayed() && e.get(0).isEnabled();
}
关于Java + Selenium : How to know if WebElement is clickable in a way other than isDisplayed, isEnabled 和 findElement?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45962207/
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
我是一名优秀的程序员,十分优秀!