如何使用Python中的Selenium正确加载网页

huangapple go评论72阅读模式
英文:

How to properly load website using selenium in python

问题

I'm trying to use selenium to open this link using the code below, but it seems to open just a very raw form of the website, with which I'm not managing to interact properly with the buttons.

options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
driver = webdriver.Chrome(service=Service(driver_path), options=options)
driver.get("https://aeromexico.com/es-mx")

wrong_result

What I wanted was for the page to load fully just as it does when I open it manually via Chrome, as the image below:

desired_result

What am I doing wrong here?

英文:

I'm trying to use selenium to open this link using the code below, but it seems to open just a very raw form of the website, with which I'm not managing to interact properly with the buttons.

options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
driver = webdriver.Chrome(service=Service(driver_path), options=options)
driver.get("https://aeromexico.com/es-mx")

wrong_result

What I wanted was for the page to load fully just as it does when I open it manually via Chrome, as the image below:

desired_result

What am I doing wrong here?

答案1

得分: 0

问题的根本原因: 您正在尝试使用Selenium访问的网站检测到了自动化机器人,因此不允许Selenium访问该网站。因此,您看到了网站的原始形式。

解决方法: 下载并导入undetected chrome driver库到您的项目中并使用它。一旦导入该库,请尝试以下代码:

import undetected_chromedriver as uc

driver = uc.Chrome()
driver.maximize_window()
driver.get("https://aeromexico.com/es-mx")

结果:

如何使用Python中的Selenium正确加载网页

有关undetected chrome driver的更多信息,请参考以下链接:

https://www.zenrows.com/blog/undetected-chromedriver#how-to

英文:

Root cause of the issue: The website you are trying to access using selenium is detecting the automation bot, and it is not allowing selenium to access the site. Hence you see the raw form of the website.

Solution: Download and import the undetected chrome driver library into your project and use it. Once you import the library try the code as below:

import undetected_chromedriver as uc

driver = uc.Chrome()
driver.maximize_window()
driver.get("https://aeromexico.com/es-mx")

Result:

如何使用Python中的Selenium正确加载网页

Refer below link for more info about undetected chrome driver:

https://www.zenrows.com/blog/undetected-chromedriver#how-to

huangapple
  • 本文由 发表于 2023年5月18日 06:39:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276627.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定