java - 元素此时不可点击 - 其他元素将收到点击
问题描述
我在网站上有一个元素,如下所示
<div id="wrapperCategory" class="categoryItemWrapper">
<div class="panel panel-default panel-categoryItem text-center categoryItem disabledItem" ng-class="category.CategoryStyleClass" ng-click="setselectedProduct(productIndex,product,category); setselectedProductAmount(null);" title="Category">
<div class="panel-heading">
Category
</div>
<div class="panel-body">
Price
</div>
<div class="panel-footer availability" ng-class="category.AvailabilityStyleClass">
<span ng-bind-html="category.AvailabilityText">Avail</span>
</div>
</div>
</div>
我需要点击它才能前进。如果我手动单击每个这些 div 或跨网站继续前进,但 SeleniumWebdriver 无法单击其中任何一个。每次出现错误时,我都尝试使用 ng-click 事件单击 span 和 div:
Exception in thread "main" org.openqa.selenium.WebDriverException: Element <div class="panel panel-default panel-categoryItem text-center categoryItem" ng-class="category.CategoryStyleClass" ng-click="setselectedProduct(productIndex,product,category); setselectedProductAmount(null);" title="Category"></div> is not clickable at point (619.2666625976562, 474.23333740234375). Other element would receive the click: <div style="top: 0;left: 0;bottom: 0;right: 0;position: fixed;pointer-events: auto !important;z-index: 10000;" id="cover-1526454140024"></div>
我不明白。我可以以某种方式检查哪个元素是可点击的,哪个元素与我想要点击的元素重叠(我在网站的代码中没有看到任何 id="cover-1526454140024" 的 div)?
更新: 不幸的是,在您的解决方案之后它仍然无法正常工作。尝试单击时出现相同的异常。
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
JavascriptExecutor js = (JavascriptExecutor) Mundial.driver;
js.executeScript("arguments[0].scrollIntoView();", categoryItem);
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(categoryItem));
categoryItem.click();
List<WebElement> selects = driver.findElements(By.tagName("select"));
Select ticketsToSelect = new Select(selects.get(3));
ticketsToSelect.selectByValue("number:2");
它仅在我进入睡眠状态并手动向下滚动时才有效。我不明白。
解决方案
根据您的回复:
您必须向下滚动才能让 web 元素可用于您的脚本。为此,您可以使用以下代码:
public static void scrollDown(WebDriver driver, String YoffSet){
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript(YoffSet);
}
在这里,您可以在代码中的任何位置调用此方法。
然后您可以使用此代码与 web 元素进行交互:
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("static ID")));
element.click();
推荐阅读
- python-3.x - 使用下一行值作为条件
- functional-programming - 如果调用作为参数给出的指定回调,方法是否仍然“纯”
- amazon-s3 - 无需在 AWS 中使用标头“x-amz-bypass-governance-retention:true”即可增加对象在治理模式下的保留
- reactjs - 背景颜色不透明度插值不起作用
- jmeter - 获取JMeter负载测试的开始时间、结束时间和测试时长
- java - 没有 Lambda 的 CompletableFuture.supplyAsync()
- c - 递归返回一个值
- javascript - AddEventListener 运行两次
- java - 在 Java 中通过名称确定项目的环境
- apache-kafka - 将 Spring 云流侦听器 (kafka) 从声明性模型迁移到函数模型