-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchromesearch.py
42 lines (31 loc) · 1.24 KB
/
chromesearch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
# Path to your ChromeDriver
# chromedriver_path = './chromedriver' # Update this path
# Initialize the Chrome driver
# driver = webdriver.Chrome(chromedriver_path)
def google_search(search_query):
service = Service()
# options = webdriver.ChromeOptions()
chrome_options = Options()
# chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument("--window-size=1280,768")
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.get("https://www.google.com")
# Locate the search box using its name attribute value
search_box = driver.find_element(By.NAME, "q")
search_box.clear()
# Type the search query
search_box.send_keys(search_query)
# Press Enter
search_box.send_keys(Keys.RETURN)
# Wait for a few seconds to see the results
time.sleep(5)
# Optionally, print the title of the page
print(driver.title)