2

I have followed the change exact but it doesn't work, are there any other bugs?

https://www.selenium.dev/blog/2023/headless-is-going-away/


import time

import pandas as pd
from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
options.headless = True

options.page_load_strategy = 'none'

chrome_path = 'chromedriver.exe'
chrome_service = Service(chrome_path)

driver = Chrome(options=options, service=chrome_service)
driver.implicitly_wait(5)

url= "https://hk.centanet.com/findproperty/list/transaction/%E6%84%89%E6%99%AF%E6%96%B0%E5%9F%8E_3-    DMHSZHHRHD?q=TiDxvVGMUUeutVzA0g1JlQ"

driver.get(url)
time.sleep(10)

contents = driver.find_element(By.CSS_SELECTOR,"div[class*='bx--structured-list-tbody']")


properties = contents.find_elements(By.CSS_SELECTOR,"div[class*='bx--structured-list-row']")

def extract_data(element):
    columns = element.find_elements(By.CSS_SELECTOR,"div[class*='bx--structured-list-td']")
    Date = columns[0].text
    Dev = columns[1].text
    Price = columns[3].text
    RiseBox = columns[4].text
    Area = columns[5].text

return{
    "Date": Date,
    "Development": Dev,
    "Consideration": Price,
    "Change": RiseBox,
    "Area": Area
}

data = []

for property in properties:
    extracted_data = extract_data(property)
    data.append(extracted_data)

df = pd.DataFrame(data)
df.to_csv("result.csv", index=False)

Comment was as follows:

Selenium.py:10: DeprecationWarning: headless property is deprecated, instead use add_argument('--headless') or add_argument('--headless=new')
options.headless = True

I tried the changes suggested by https://www.selenium.dev/blog/2023/headless-is-going-away/ but it doesn't work

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
YPT
  • 21
  • 2
  • 1
    The warning says to use `add_argument('--headless')` or `add_argument('--headless=new')` but you are doing `options.headless = True` – It_is_Chris Feb 06 '23 at 17:37
  • 1
    Thanks! but i have tried all of them just didnt put in the code – YPT Feb 07 '23 at 02:28

2 Answers2

0

Try this configuration:

# pip install selenium==4.8.0    

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

options=Options()
options.add_argument("--headless=new")    
options.add_experimental_option("excludeSwitches", ["enable-logging"]) #Optional
service=Service(f"path\chromebrowser.exe")

browser=webdriver.Chrome(service=service, options=options)

print(browser.name)

  • 1
    thanks. I have just tied your code and the following comment returned: ------------------------------------------------------- C:\Users\user\PycharmProjects\pythonProject2\venv\Scripts\python.exe "C:\Users\user\PycharmProjects\pythonProject2\venv\中原python Selenium.py" Traceback (most recent call last): File "C:\Users\user\PycharmProjects\pythonProject2\venv\中原python Selenium.py", line 11, in browser=webdriver.Chrome(service=service, options=options) ^^^^^^^^^ NameError: name 'webdriver' is not defined Process finished with exit code 1 – YPT Feb 10 '23 at 17:29
  • Add the first line of import, my bad there. – Angelo Entrepreneur Feb 12 '23 at 01:38
0

This warning message...

DeprecationWarning: headless property is deprecated, instead use add_argument('--headless') or add_argument('--headless=new') options.headless = True

...is inline with the Selenium Blog post with the heading Headless is Going Away!


Details

Chromium team have released the Native Headless mode which is now officially the new Headless mode. This functionality is available with:

  • Chromium v109.0.5400.0
  • ChromeDriver v109.0.5414.25
  • Selenium v4.8.0

The new syntax requires --headless=new to be passed as an argument as follows:

  • Java:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--headless=new");
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://selenium.dev);
    driver.quit();
    
  • Python:

    options = ChromeOptions()
    options.add_argument("--headless=new")
    driver = webdriver.Chrome(options=options)
    driver.get('http://selenium.dev')
    driver.quit()
    
  • Javascript:

    let driver = await env
      .builder()
      .setChromeOptions(options.addArguments('--headless=new'))
      .build();
    await driver.get('https://selenium.dev');
    await driver.quit();
    
  • CSharp:

    var options = new ChromeOptions();
    options.AddArgument("--headless=new");
    var driver = new ChromeDriver(options);
    driver.Navigate().GoToUrl("https://selenium.dev");
    driver.Quit();
    
  • Ruby:

    options = Selenium::WebDriver::Options.chrome(args: ['--headless=new'])
    driver = Selenium::WebDriver.for :chrome, options: options
    driver.get('https://selenium.dev')
    driver.quit
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352