首页 > 解决方案 > Selenium webdriver 在无头 Firefox 中缺少点击

问题描述

我有一个在 Ubuntu Linux(16.04.4 LTS)上使用 Firefox(v59.0.2 64 位)用 python(3.5.2)编写的硒测试。geckodriver 版本为 v0.20.1。我在 Twitter 上自动发布一条推文,在 GUI 模式下它每次都能完美运行。然而,在无头模式下,它每次都会错过对特定元素的相同点击。有没有人见过这个?还有关于在无头模式下调试的任何建议吗?

from selenium.webdriver.support import expected_conditions as ec

# Here is the way I am getting the element
@property
def sending_message_close_button(self):
    self._sending_tweets_message_close = self._driver.wait.until(ec.element_to_be_clickable((By.CSS_SELECTOR,
                                                                                             "div.alert-messages.js-message-drawer-visible a.Icon.Icon--close.Icon--medium.dismiss")))
    return self._sending_tweets_message_close

这是在同一个类中的方法中单击元素的调用:

self.sending_message_close_button.click()

我尝试使用 ActionBuilder 移动到元素并单击它。我也尝试过点击 javascript。都没有奏效。

以下是我初始化浏览器的方式(为保护隐私,url 已编辑):

@property
def webdriver(self):
    if self._webdriver is None:
        options = Options()
        options.add_argument('-headless')
        profile = FirefoxProfile()
        profile.set_preference("network.proxy.type", 2)
        if self._env == 'demo':
            profile.set_preference("network.proxy.autoconfig_url", "")
        elif self._env == 'stage':
            profile.set_preference("network.proxy.autoconfig_url", "")
        profile.set_preference("network.proxy.no_proxies_on", "localhost")
        if self._use_options:
            self._webdriver = webdriver.Firefox(firefox_profile=profile, options=options)
        else:
            self._webdriver = webdriver.Firefox(firefox_profile=profile)
        self._webdriver.implicitly_wait(10)
    return self._webdriver

标签: python-3.xseleniumubuntufirefoxheadless

解决方案


推荐阅读