问题描述
我想点击菜单链接但没有运气。它总是显示异常 -
I am trying to click on a menu link but not have any luck. It always showing exception -
我有以下 html
snippet
I have following html
snippet
<div id="RWR" class="clsDesktopHome" style="position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px; overflow: auto;">
<div class="clsDesktop clsDesktopHomePage" style="width: 1553px; height: 430px; top: 0px; left: 15px;">
<div id="foid:2" class="clsDesktopHeader clsTextOnDesktopColor">
<div id="foid:1" class="clsDesktopTabs" style="margin-right: 230px; height: 28px; visibility: visible; width: auto;">
<span class="clsDesktopTab clsDesktopTabActive clsDesktopTabTypeHome clsDesktopTabTypeHomeActive">
<span class="clsDesktopTabContent">
<span class="clsDesktopTabTypeIcon"></span>
<span class="clsDesktopTabMenuIcon"></span>
<span class="clsDesktopTabCollaborationIcon"></span>
<span class="clsDesktopTabCaption">Home</span>
<span class="clsDesktopTabCloseIcon"></span>
</span>
</span>
<span class="clsDesktopTab clsDesktopTabInactive clsDesktopTabCanClose clsDesktopTabTypeSheet">
<span class="clsDesktopTab clsDesktopTabInactive clsDesktopTabCanClose clsDesktopTabTypeSheet">
<span class="clsDesktopTab clsDesktopTabInactive clsDesktopTabCanClose clsDesktopTabTypeSheet">
<span class="clsDesktopTab clsDesktopTabInactive clsDesktopTabCanClose clsDesktopTabTypeSheet">
<span class="clsDesktopTab clsDesktopTabInactive clsDesktopTabCanClose clsDesktopTabTypeSheet">
<span class="clsDesktopTab clsDesktopTabInactive clsDesktopTabCanClose clsDesktopTabTypeSheet">
<span class="clsDesktopTab clsDesktopTabInactive clsDesktopTabCanClose clsDesktopTabTypeSheet">
<span class="clsDesktopTab clsDesktopTabHidden clsDesktopTabNoCaption clsDesktopTabTypeTabsMenu">
<span class="clsDesktopTab clsDesktopTabInactive clsAddNewContainer clsDesktopTabTypeAddNew">
</div>
<div class="clsDesktopBelowTabs" style="height: 325px; visibility: visible;">
<div id="foid:2" class="clsDesktopFooter clsTextOnDesktopColor" style="height: 18px; line-height: 18px;">
</div>
<div class="clsModalNode" style="position: absolute; left: 0px; top: 0px; width: 0px; height: 0px; z-index: 10; background-color: rgb(0, 0, 0);"></div>
<div style="position: absolute; left: 0px; top: 0px; width: 100%; height: 100%; z-index: 30; background-color: rgb(221, 221, 221); opacity: 0.4; display: none;"></div>
</div>
这是快照的样子 -
And this is the snapshot how it looking like -
我正在使用以下代码来实现相同的目标 -
I'm using following code to accomplish the same -
WebElement element = driver.findElement(By.xpath(".//*[@id='foid:1']/span[1]/span/span[4]"));
WebDriverWait wait = new WebDriverWait(driver, 120);
wait.until(ExpectedConditions.elementToBeClickable(element));
//driver.findElement(By.xpath("//span[contains(text(), 'Home')]")).click();
driver.findElement(By.xpath(".//*[@id='foid:1']/span[1]/span/span[4]")).click();
我检查了< div>
DOM中接受点击的标签。但是我看到了这个
I did inspect the <div>
tag in DOM which accepting the click. But I'm seeing this
<div style="position: absolute; left: 0px; top: 0px; width: 100%; height: 100%; z-index: 30; background-color: rgb(221, 221, 221); opacity: 0.4;"></div>
带有一个额外属性,即 display:none;
with one additional attribute i.e. display:none;
使用以下配置:
-
Selenium 3.0 .1
- 驱动程序 -
ChromeDriver
Selenium 3.0.1
- Driver -
ChromeDriver
我不知道要处理这种情况。
I don't know to to handle this situation.
推荐答案
尝试等到元素点击消失了:
Try to wait until element that gets click disappeared:
new WebDriverWait(driver, 10).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath('//div[@style="position: absolute; left: 0px; top: 0px; width: 100%; height: 100%; z-index: 30; background-color: rgb(221, 221, 221); opacity: 0.4;"]')));
由于这个答案被低估了,我添加了一些细节来解释为什么它可以成为可接受的解决方案。
As this answer was downvoted, I add some more details to explain why it could be acceptable solution.
chromedriver
: chromedriver这是一个已知的问题(我个人已经面临过几次)
有时会忽略模态窗口,例如正在加载页面
It's a known issue (I personally have faced it few times) of chromedriver
: chromedriver
sometimes ignores modal windows such as "Page loading in progress"
并认为目标元素(由模态窗口覆盖)实际上是可见且可点击的,并尝试进行模态窗口接收的点击。
and "thinks" that target element (that is covered by modal window) actually visible and clickable and tries to make click which is received by modal window.
所以等到模态窗口消失是有道理的。
So it makes sense to wait until modal window disappeared.
这篇关于点击菜单链接时,获取元素不是可点击的例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!