Python Forum
Push Button on website - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Push Button on website (/thread-41555.html)



Push Button on website - cg1980 - Feb-05-2024

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()



RE: Push Button on website - snippsat - Feb-07-2024

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()