【问题标题】:Geckodriver can't click on element | python 3 CodingGeckodriver 无法点击元素 | python 3 编码
【发布时间】:2018-11-30 23:07:06
【问题描述】:

正如主题中所说,我正在尝试单击找到的元素,但取决于 geckodriver 和 firefox(适用于 Linux)版本,我得到 2 个结果。对于旧版本(如 FF38 + geckodriver 0.15 -> 0.17.0),我有类似报告的here

元素被点击,新窗口出现,但动作并未终止。 Geckodriver 挂起点击。

对于较新的版本(FF52 ESR -> FF60 ESR + geckodriver 0.19.0 -> 0.21.0)点击无效...当然我不能使用 send_keys() 方法。

这里是元素:

<span onclick="modalClientPopup('http://myserver.local/target', null, 800, null, false);">
    <img src="img/target.gif" title="Target" alt="Target" onmouseover="iconOnmouseover(this, 0);" onmouseout="iconOnmouseout(this, 0);">
    <br>
    <label id="home_label_0" title="Target" style="text-decoration: underline; cursor: auto; font-weight: normal;" onmouseover="iconOnmouseover(this, 0);" onmouseout="iconOnmouseout(this, 0);">
        Target folder
    </label>
</span>

代码如下:

iframe = browser.find_element_by_xpath("//iframe[@keyid='1/ACCUEIL']")
browser.switch_to.frame(iframe)
focus_lnk = browser.find_element_by_xpath("//label[@title='Target']")
window = browser.window_handles
print(len(window))
action = ActionChains(browser)
action.move_to_element(focus_lnk)
action.click(focus_lnk)
action.perform()
print(len(window))

显示第一次打印,但不显示第二次。如果我使用 focus_lnk.click(),结果是一样的。

最好使用 55+ 版本,以获得无头选项。 是否有解决方法/解决方案?

【问题讨论】:

  • 如果你得到它,请分享异常详细信息
  • 不幸的是,什么都没有出现,因为显然没有结束/超时......这就是让我陷入困境的原因。

标签: python selenium web-scraping geckodriver


【解决方案1】:

modalClientPopup() 指的是 window.showModalDialog(),它已被弃用。

所以我用一段 JavaScript 即时修改了 html 代码:

browser.execute_script("""
setInterval (function() { override (); }, 500);

function override () {
    var divElem;
    var innerDoc;
    var focus;
    divElem = document.getElementsByClassName('content_sub_zone');
    innerDoc = divElem[0].childNodes[0].contentDocument;
    focus = innerDoc.getElementsByTagName('span')[0];
    focus.setAttribute("onclick", "window.open('http://myserver.local/target', '', 'width=800,height=400');");
                    }
                    """)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 2018-04-02
    • 2019-12-19
    相关资源
    最近更新 更多