我假设Selenium打开的Chrome浏览会话与Google Chrome本地安装相同.但是当我尝试在这个网站上搜索时,即使只是用selenium打开它并手动控制搜索过程,我会收到一条错误消息,当我使用我自己的个人资料或在隐身窗口中使用常规chrome时,搜索结果会返回正常.
每当我搜索这个问题时,我会发现说明鼠标移动或点击模式的结果会将其丢弃.但事实并非如此,因为我在打开浏览器后尝试手动控制. html请求中的某些内容将其丢弃.无论如何要克服这个问题吗?
有问题的网站是:
https://www.avnet.com/wps/portal/us
最佳答案 #TooLongForComment
重现此问题
from random import randint
from time import sleep
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument('--disable-infobars')
options.add_argument('--disable-extensions')
options.add_argument('--profile-directory=Default')
options.add_argument('--incognito')
options.add_argument('--disable-plugins-discovery')
options.add_argument('--start-maximized')
browser = webdriver.Chrome('./chromedriver', chrome_options=options)
browser.get('https://www.avnet.com/wps/portal/us')
try:
search_box_id = 'searchInput'
myElem = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, search_box_id)))
elem = browser.find_element_by_id(search_box_id)
sleep(randint(1, 5))
s = 'CA51-SM'
for c in s: # randomize key pressing
elem.send_keys(c)
sleep(randint(1, 3))
elem.send_keys(Keys.RETURN)
except TimeoutException as e:
pass
finally:
browser.close()
我用hexedit编辑了从$cdc_到fff的chromedriver键.
>通过阅读每个JavaScript块来调查它是如何完成的,请查看此answer以获取检测示例
>尝试通过更改user-agent&添加扩展以修改Googlebot下的标题和掩码. referrer options.add_extension(‘/ path-to-modify-header-extension’)