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只是在寻找硒的解决方案。
发布于 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
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())然后在运行它时:
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.68shttps://stackoverflow.com/questions/73667721
复制相似问题