When I try selenium option argument in my PC, with either of modes (--headerless) or (--silent) it works fine. But on clients device it interrupts with strange code. I even added windows size after arguments, but error is same.Error with Feature-Policy header
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument('--headerless')
webdriver_service = Service("chromedriver.exe")
driver = webdriver.Chrome(service=webdriver_service, options=chrome_options)
driver.get(url)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "onetrust-accept-btn-handler"))).click()
CodePudding user response:
The website with selenium headless browser mode detected as bot.
To avoid detection largely depends on
maximize_window_size()
In your case,It's also need to add
--disable-blink-features=AutomationControlled
The following example is working fine in Headless Chrome Selenium:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.headless = True
options.add_argument("start-maximized")
#options.add_experimental_option("detach", True)
options.add_argument("--no-sandbox")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
webdriver_service = Service("./chromedriver") #Your chromedriver path
driver = webdriver.Chrome(service=webdriver_service,options=options)
url = 'https://soundcloud.com/daydoseofhouse/snt-whats-wrong/s-jmbaiBDyQ0d?si=233b2f843a2c4a7c8afd6b9161369717&utm\_source=clipboard&utm\_medium=text&utm\_campaign=social\_sharing'
driver.get(url)
cookie = WebDriverWait(driver, 15).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="onetrust-accept-btn-handler"]'))).click()
video_duration = WebDriverWait(driver, 15).until(EC.visibility_of_element_located((By.XPATH, '//div[@]/span[2]'))).text
print(video_duration)
Output:
2:51