代码之家  ›  专栏  ›  技术社区  ›  julian bechtold

用Selenium chromedriver单击链接<a href=“javascript:;”..>,在C中返回错误“element not visible”#

  •  0
  • julian bechtold  · 技术社区  · 6 年前

    我有一个在div中带有链接的网站。该网页的html源代码检查如下所示:

    <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>
    

    enter image description here

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

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

    var link = buttonWrapper.FindElement(By.ClassName("trigger-remove-all-sales"));
    link.Click();
    

    错误消息如下所示:


    铬驱动=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),平台=Windows NT 10.0.17134 x86英寸(64英寸)

    对如何进行有什么建议吗?

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

    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();
    

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

    根据建议,我尝试使用.FindElement()定位元素 Displayed=false

    enter image description here

    我添加了一个 Thread.Sleep(120000) 在两者之间,我们重新查找代码,看看会有什么变化。这是测试代码:

    var buttonWrapper = browser.FindElement(By.ClassName("add-to-cart-wrapper"));
    var link = buttonWrapper.FindElement(By.ClassName("trigger-remove-all-sales"));
    Thread.Sleep(120000);
    buttonWrapper = browser.FindElement(By.ClassName("add-to-cart-wrapper"));
    link = buttonWrapper.FindElement(By.ClassName("trigger-remove-all-sales"));
    

    即使在等待2分钟之后,元素仍然被找到,但仍然显示为 显示=假

    在google Chrome的开发者控制台中,我观察了元素并发现了以下属性: enter image description here

    3 回复  |  直到 6 年前
        1
  •  0
  •   iamsankalp89    6 年前

    你必须等到元素不可见,它是java代码,你也可以使用任何定位器,无论是XPath或id或类名

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

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

    转换这个代码在C夏普,因为我没有很好的熟练程度在它

        2
  •  0
  •   julian bechtold    6 年前

    我做了一个非常,非常肮脏的解决办法,移动鼠标位置,然后单击位置而不是元素。

    但是这个解决方案真的很肮脏,而且当网站上发生变化时,很容易出错。因此,我不想将此标记为答案:

    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() 所以你不会无意中点击链接。鼠标追踪脚本必须在每次页面刷新时重新加载,这样当你点击一个链接时,你就不能再追踪你的鼠标了。此外,只有在鼠标移动之前初始化脚本时,脚本才会工作。。

        3
  •  0
  •   julian bechtold    6 年前

    我使用的解决方案是使用scriptexecutor来单击有效的元素:

    var button = browser.FindElement(By.ClassName("add-to-cart-wrapper"));
    ((IJavaScriptExecutor)browser).ExecuteScript("arguments[0].click();", button);