如何在 selenium 中使用无头 Chrome 启用 JavaScript

2024-01-30

import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import time

def checkLinkedIn(command):
    url = f"https://www.linkedin.com/in/{command}"
    path = "C:\Program Files (x86)\chromedriver.exe"
    options = Options()
    options.add_argument("--headless")
    driver = webdriver.Chrome(path, options=options)
    driver.get(url)
    soup = BeautifulSoup(driver.page_source, 'html.parser')
    time.sleep(2)
    driver.quit()
    name = soup.find("h1", attrs={"class": "top-card-layout__title"})
    if name:
        print("LinkedIn profile found")
        print(url)
    else:
        print("No LinkedIn profile found")

def checkTwitter(command):
    url = f"https://www.twitter.com/{command}"
    path = "C:\Program Files (x86)\chromedriver.exe"
    options = Options()
    options.add_argument("--headless")
    driver = webdriver.Chrome(path, options=options)
    driver.get(url)
    soup = BeautifulSoup(driver.page_source, 'html.parser')
    time.sleep(2)
    driver.quit()
    at_tag = soup.find("div", attrs={"dir": "ltr"})
    print(soup.text)
    if at_tag:
        print("Twitter profile found")
        print(url)
    else:
        print("No Twitter profile found")

usrname = input("--> ")

checkTwitter(usrname)

LinkedIn 功能有效。然而,推特上却提出了这一点:

JavaScript 不可用。 我们检测到该浏览器中禁用了 JavaScript。请启用 JavaScript 或切换到支持的浏览器以继续使用 twitter.com。您可以在我们的帮助中心查看支持的浏览器列表。

如何在无头 Chrome 中启用 Javascript?提前致谢。


这可能是因为该网站检测到它是无头浏览器并禁用了某些功能。

为了解决这个问题,您可以(尽可能地)欺骗无头浏览器的身份来欺骗网站。

尝试以下选项:

from fake_useragent import UserAgent

options = webdriver.ChromeOptions()

options.add_argument('--headless')
options.add_argument("--incognito")
options.add_argument("--nogpu")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1280,1280")
options.add_argument("--no-sandbox")
options.add_argument("--enable-javascript")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')

ua = UserAgent()
userAgent = ua.random

driver = webdriver.Chrome(options=options)
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": userAgent})

这对我来说对一个特别顽固的网站很有用。 我从许多答案中收集到的选项,特别是这个:https://stackoverflow.com/a/53040904/5339857 https://stackoverflow.com/a/53040904/5339857

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 selenium 中使用无头 Chrome 启用 JavaScript 的相关文章