单击链接<a href="javascript:;" ...=""> with Selenium chromedriver returns error &#39;element not visible&#39; in C#

时间:2018-09-12 12:21:06

标签: c# selenium selenium-chromedriver

I have a website with a link inside a div. The html source inspection from the webpage looks like this:

<div class="add-to-cart-wrapper">
    <button data-tooltip="Only investments from the selected page will be sold." data-theme="dark" data-placement="top" data-tooltip-trigger="hover" data-id-list="48999040" class="btn btn-default tooltip-item trigger-sell-all">
        Sell All
    </button>
    <a href="javascript:;" data-tooltip="Remove all investments from sale" data-placement="top" data-theme="dark" data-tooltip-trigger="hover" data-currency-iso-code="978" class="trigger-remove-all-sales">
        <i class="fas fa-reply-all fa-flip-vertical"></i>
    </a>
</div>

In the browser it looks like this:

enter image description here

我实际上想归档的是单击“全部出售”按钮下方的那两个灰色箭头。哪个与filter

相匹配

不幸的是,以下用于Selenium的C#代码(Chromedriver)返回了该元素当前不可见的情况,尽管我可以在浏览器中手动单击它:

 re = ['\n', '\n', '0 / 6\n', '1 / 6\n', '2 / 6\n', '3 / 6\n', '4 / 6\n', '5 / 6\n', '6 / 6\n', '\n', 'mobile\n']

resul = map(str.rstrip, re)
str_list = filter(None, list(resul))
print(list(str_list))

# ['0 / 6', '1 / 6', '2 / 6', '3 / 6', '4 / 6', '5 / 6', '6 / 6', 'mobile']

错误消息如下:

  

OpenQA.Selenium.ElementNotVisibleException:'元素不可见
  (会话信息:chrome = 68.0.3440.106)(驱动程序信息:   chromedriver = 2.41.578737   (49da6702b16031c40d63e5618de03a32ff6c197e),平台= Windows NT   10.0.17134 x86_64)'

有关如何进行的任何建议?

编辑: 按照建议,我按照下面的代码添加了一个“ wait.Until(“元素x是可见的”)'。不幸的是,我遇到了超时错误:

<a href="javascript:;" [...]>
  

OpenQA.Selenium.WebDriverTimeoutException:'20秒后超时'

根据建议,我尝试使用.FindElement()定位元素 我注意到的是属性var link = buttonWrapper.FindElement(By.ClassName("trigger-remove-all-sales")); link.Click();

enter image description here

我在两者之间添加了new WebDriverWait(browser, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("div.add-to-cart-wrapper a.trigger-remove-all-sales[data-tooltip='Remove all investments from sale']"))).Click(); ,并重新查找了代码以查看会发生什么变化。这是测试代码:

Displayed=false

即使经过2分钟的等待时间,该元素仍被找到,但仍显示为Thread.Sleep(120000)

在Google Chrome浏览器的开发者控制台中,我观察了元素并找到以下属性: enter image description here

有一个事件监听器,可以使用a.trigger-remove-all-sales

3 个答案:

答案 0 :(得分:0)

您必须等到该元素不可见为止,它是java代码,您还可以使用任何定位符,无论是XPath还是id或类名

WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement elem = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("")));
elem.click();

或者您也可以使用elementToBeClickable()方法

WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement elem = wait.until(ExpectedConditions.elementToBeClickable(By.id("")));
elem.click();

由于我不熟练,因此将其转换为C语言。

答案 1 :(得分:0)

我做了一个非常非常肮脏的解决方法,方法是移动鼠标位置并在该位置而不是元素上单击。

但是,此解决方案确实很脏,并且在网站上发生某些更改时容易出错。因此,我不想将此标记为答案:

new Actions(browser).MoveToElement(link, 1225, 565).Click().Build().Perform();

要查看鼠标的实际位置,您可以在此行之前设置一个暂停/中断标记,打开Browserdriver,然后将以下js代码粘贴到开发者控制台中:

// Create mouse following image.
var seleniumFollowerImg = document.createElement("img");

// Set image properties.
seleniumFollowerImg.setAttribute('src', 'data:image/png;base64,'
+ 'iVBORw0KGgoAAAANSUhEUgAAABQAAAAeCAQAAACGG/bgAAAAAmJLR0QA/4ePzL8AAAAJcEhZcwAA'
+ 'HsYAAB7GAZEt8iwAAAAHdElNRQfgAwgMIwdxU/i7AAABZklEQVQ4y43TsU4UURSH8W+XmYwkS2I0'
+ '9CRKpKGhsvIJjG9giQmliHFZlkUIGnEF7KTiCagpsYHWhoTQaiUUxLixYZb5KAAZZhbunu7O/PKf'
+ 'e+fcA+/pqwb4DuximEqXhT4iI8dMpBWEsWsuGYdpZFttiLSSgTvhZ1W/SvfO1CvYdV1kPghV68a3'
+ '0zzUWZH5pBqEui7dnqlFmLoq0gxC1XfGZdoLal2kea8ahLoqKXNAJQBT2yJzwUTVt0bS6ANqy1ga'
+ 'VCEq/oVTtjji4hQVhhnlYBH4WIJV9vlkXLm+10R8oJb79Jl1j9UdazJRGpkrmNkSF9SOz2T71s7M'
+ 'SIfD2lmmfjGSRz3hK8l4w1P+bah/HJLN0sys2JSMZQB+jKo6KSc8vLlLn5ikzF4268Wg2+pPOWW6'
+ 'ONcpr3PrXy9VfS473M/D7H+TLmrqsXtOGctvxvMv2oVNP+Av0uHbzbxyJaywyUjx8TlnPY2YxqkD'
+ 'dAAAAABJRU5ErkJggg==');
seleniumFollowerImg.setAttribute('id', 'selenium_mouse_follower');
seleniumFollowerImg.setAttribute('style', 'position: absolute; z-index: 99999999999; 
pointer-events: none;');

// Add mouse follower to the web page.
document.body.appendChild(seleniumFollowerImg);

// Track mouse movements and re-position the mouse follower.
$(document).mousemove(function(e) {
$("#selenium_mouse_follower").css({ left: e.pageX, top: e.pageY });
});

在鼠标移动代码之后,您可以设置另一个断点,以查看鼠标在浏览器中实际移动的位置。

提示:删除.Click(),这样就不会意外单击链接。每次页面刷新时都必须重新加载mousetracking脚本,因此当您单击链接时,就无法再跟踪鼠标。同样,该脚本仅在鼠标移动之前初始化时才有效。.

答案 2 :(得分:0)

我使用的解决方案是使用脚本执行器单击有效的元素:

null