3

I was trying to scrape the snippet text from google search page and this solution worked well. The only issue I have now is that the text is in Bangla while I want it in English.

Here's what I've tried:

options = webdriver.ChromeOptions()
options.add_argument('lang=en')

driver = webdriver.Chrome(executable_path=r'the\path\for\chromedriver.exe', options=options)

I've tried adding 'lang=en' as an argument to ChromeOptions and pass it to webdriver.Chrome(). That's all I could figure out but it's not working.

Here's the full code:

from selenium import webdriver
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_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'})
options.add_argument('lang=en')

driver = webdriver.Chrome(executable_path=r'C:\Users\camoh\AppData\Local\Programs\Python\Python38\chromedriver.exe', options=options)

driver.get('https://google.com/')
assert "Google" in driver.title
#wait = WebDriverWait(driver, 20)
#wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".gLFyf.gsfi")))
input_field = driver.find_element_by_css_selector(".gLFyf.gsfi")
input_field.send_keys("when barack obama born")
input_field.send_keys(Keys.RETURN)

#wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".Z0LcW.XcVN5d")))
result = driver.find_element_by_css_selector(".Z0LcW.XcVN5d").text
print(result)
driver.close()
driver.quit()

Here's the page when I run the code:

seelnium output

ganjaam
  • 675
  • 1
  • 11
  • 25

2 Answers2

2

You can try with below code to add argument with preferred language:

from selenium.webdriver.chrome.options import Options as ChromeOptions #import library
options=webdriver.ChromeOptions() #create object of ChromeOptions 
options.add_argument("--lang=en")   
options.add_argument("--lang=en-US")#or you can use 
Bhavya Parikh
  • 2,818
  • 2
  • 7
  • 18
1

Use -

options.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'})
Swaroop Humane
  • 1,570
  • 1
  • 6
  • 16