首页 > 解决方案 > Selenium chrome --headless 如何工作?

问题描述

伙计们!与 selenium 一起使用的 chromedriver 中的基本模式和 --headless 模式之间的真正区别是什么?我问是因为我试图在 adidas.com 上跟踪一些价格,当我设置我的驱动程序时:

chrome_options = webdriver.ChromeOptions()
browser = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)
browser.get(url)

浏览器在我的电脑上打开,一切正常。

但是当我试图执行下面的代码时:

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
browser = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)
browser.get(url)

回应是:

'<html lang="en"><head>\n    <meta charset="utf-8">\n    <meta name="viewport"
content="width=device-width, initial-scale = 1.0, maximum-scale=1.0, user- 
scalable=no">\n    <title>adidas</title>\n\n    <link 
href="/WAFfailoverassets/style.css" rel="stylesheet">\n</head>\n<body>\n\n<main 
class="content-wrapper">\n    <header class="top">\n        <div 
class="logo">adidas</div>\n        <span 
id="ctl00_mainContentPlaceHolder_lblInvalidRequest">\n<br> Reference Error: 
18.6c85655f.1629459608.2c05a32c </span>\n        <h1 class="title">Unfortunately we 
are unable to give you access to our site at this time. </h1>\n        <p>A security 
issue was automatically identified, when you tried to access the website.</p>\n    
</header>\n\n    <div class="columns">\n\n        <section class="column column-1">\n            
<h3>What could have caused this?</h3>\n            <p>During high-traffic product 
releases we have extra security in place to prevent bots entering our site. We do 
this to protect customers and to give everyone a fair chance of getting the sneakers. 
Something in your setup must have triggered our security system, so we cannot allow 
you onto the site.</p>\n        </section>\n\n        <section class="column column- 
2">\n            <h3>How can I resolve this?</h3>\n            <p>Please try 
refreshing or accessing our website from a different browser or another device. You 
could also check if there are any scripts, like ad blockers, running in your browser 
and disable them. </p>\n            <p>If this does not help, the issue might be 
caused by malware. If you\'re on a personal connection you can run an anti-virus scan 
on your device to make sure it\'s not affected by malware. If you\'re on a shared 
network, you could ask the system administrator to run a scan looking for 
misconfigured or infected devices across the network.</p>\n        </section>\n\n    
</div>\n\n    <small class="error">HTTP 403 - 
Forbidden</small>\n\n</main>\n\n\n\n</body></html>' 

所以问题是网站如何检测它的代码是否真的呈现,如果我想在服务器上运行我的代码,我该如何处理?

标签: python-3.xseleniumweb-scrapingselenium-chromedriverundetected-chromedriver

解决方案


推荐阅读