[英]Undetected Chromedriver issue when headless is on
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.hyatt.com/')
if headless = True
the site isn't responsive.如果
headless = True
则站点没有响应。 How can i solve that?我该如何解决?
PS am seeking solution for selenium only.
PS 我只为 selenium 寻求解决方案。
Use a virtual display such as Xvfb so that you don't need to use headless mode on a headless machine such as Linux servers.使用 Xvfb 等虚拟显示器,这样您就无需在 Linux 服务器等无头机器上使用无头模式。
There's a Selenium Python framework, https://github.com/seleniumbase/SeleniumBase , with built-in integration to undetected-chromedriver for that exact thing.有一个 Selenium Python 框架, https://github.com/seleniumbase/SeleniumBase ,内置用于未检测到的东西的集成驱动程序When running your tests, add
--uc
as a pytest command-line option for your SeleniumBase tests.运行测试时,将
--uc
添加为 SeleniumBase 测试的 pytest 命令行选项。 Eg:例如:
pytest --uc --xvfb
That lets you successfully run Selenium Python tests on a Linux headless machine in undetected-chromedriver mode.这使您可以在未检测到的 chromedriver 模式下在 Linux 无头机器上成功运行 Selenium Python 测试。 (With SeleniumBase )
(使用SeleniumBase )
You can use the following for your test, test_hyatt.py
:您可以使用以下内容进行测试
test_hyatt.py
:
from seleniumbase import BaseCase
class MyTestClass(BaseCase):
def test_hyatt(self):
self.open("https://www.hyatt.com/")
self.assert_in("Reservations", self.get_title())
print(self.get_title())
Then when running it:然后在运行时:
pytest test_hyatt.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
hyatt.py Hotel Reservations | Book Hotel Rooms Online - Hyatt Hotels and Resorts
.
==================== 1 passed in 8.68s
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.