I'm trying to create a short Google search automation script using Selenium in Python. Here's my script at the moment:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome(executable_path="/Users/p1/Environments/p1_env/chromedriver")
driver.implicitly_wait(0.5)
#launch URL
driver.get("https://www.google.com/")
#identify search box
m = driver.find_element_by_name("q")
lst = []
with open('test.txt') as f:
for line in f:
print(line)
#enter search text
m.send_keys(lst)
time.sleep(0.2)
#perform Google search with Keys.ENTER
m.send_keys(Keys.ENTER)
Essentially, I'd like to have the script read a text file line by line and search the list individually. Eventually, I hope to print a Yes if a specific search is found and no if not. First, I need to get the read txt loop created first.
CodePudding user response:
Just go through the test file and send each line to the search input tag
with open('test.txt') as f:
for line in f:
driver.get("https://www.google.com/")
#identify search box
m = driver.find_element_by_name("q")
#enter search text
m.send_keys(line)
time.sleep(0.2)
#perform Google search with Keys.ENTER
m.send_keys(Keys.ENTER)