Python Forum

Full Version: Push Button on website
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I trying to do a script that open and push on a button automatically when run. As exemple I tried to open google.com and push on "google search automatically) but it doesnt work. Below is my code

import time
import webbrowser
from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager

 
driver = webdriver.Chrome()
url = "https://www.google.com/"
driver.get(url)

button = driver.find_element_by_xpath(//*[@id="L2AGLb"]/div)
clicking on the button
button.click()
Most first push accept all button.
Then input some text and search.
Code is old,have to setup chrome driver(in a OS path) first in code.
Old way find_element_by_xpath new find_element(By.XPATH, 'tag').
Here a working test.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time

# Setup
#https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/121.0.6154.0/win64/chromedriver-win64.zip"
options = Options()
#options.add_argument("--headless=new")
ser = Service(r"C:\cmder\bin\chromedriver.exe")
browser = webdriver.Chrome(service=ser, options=options)
# Parse or automation
url = "https://www.google.com/"
browser.get(url)
time.sleep(3)
accept_bt = browser.find_element(By.CSS_SELECTOR, '#L2AGLb')
accept_bt.click()
input_field = browser.find_element(By.CSS_SELECTOR, '#APjFqb')
input_field.send_keys('car')
time.sleep(3)
search_bt = browser.find_element(By.XPATH, 'input.gNO89b')
search_bt.click()