首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当无头打开时未被检测到的Chromedriver问题

当无头打开时未被检测到的Chromedriver问题
EN

Stack Overflow用户
提问于 2022-09-09 21:36:49
回答 1查看 463关注 0票数 1
代码语言:javascript
复制
import undetected_chromedriver.v2 as uc
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


def main(url):
    options = uc.ChromeOptions()
    options.headless = True
    driver = uc.Chrome(options=options)
    driver.get(url)
    try:
        WebDriverWait(driver, 10).until(
            EC.title_contains(('Reservations'))
        )
        print(driver.title)
    except Exception as e:
        print(type(e).__name__)
    finally:
        driver.quit()


if __name__ == "__main__":
    main('https://www.example.com/')

如果headless = True网站没有响应。我该怎么解决呢?

P.S只是在寻找硒的解决方案。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-11 16:03:03

使用像Xvfb这样的虚拟显示,这样就不需要在无头机器(如Linux服务器)上使用无头模式。

有一个Selenium框架,https://github.com/seleniumbase/SeleniumBase,内置集成到未被检测到的chromedriver中。在运行测试时,添加--uc作为SeleniumBase测试的pytest命令行选项。例:

pytest --uc --xvfb

这使您能够在Linux无头计算机上以未检测到的chromedriver模式成功地运行Selenium测试。(与SeleniumBase)

您可以在您的测试中使用以下内容,example.py

代码语言:javascript
复制
from seleniumbase import BaseCase

class MyTestClass(BaseCase):
    def test_hyatt(self):
        self.open("https://www.example.com/")
        self.assert_in("Reservations", self.get_title())
        print(self.get_title())

然后在运行它时:

代码语言:javascript
复制
pytest example.py --uc --xvfb
==================== test session starts ====================
platform darwin -- Python 3.10.5, pytest-7.1.3, pluggy-1.0.0
rootdir: /Users/michael/github/SeleniumBase/examples, configfile: pytest.ini
plugins: html-2.0.1, xdist-2.5.0, forked-1.4.0, rerunfailures-10.2, ordering-0.6, cov-3.0.0, metadata-2.0.2, seleniumbase-4.3.8
collected 1 item                                                                                   

example.py Example Domain
.

==================== 1 passed in 8.68s
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73667721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档