I am a bit new to tkinter and I tried to use selenium and tkinter for building an interface but opened webpage in chrome closed unexpectedly, any ideas ?
import tkinter as tk
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.action_chains import ActionChains
root= tk.Tk()
canvas1 = tk.Canvas(root, width = 300, height = 300)
canvas1.pack()
def hello ():
driver = webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe")
driver.get("https://google.com")
search = driver.find_element_by_xpath("//input[@type='text']")
search.send_keys('hello')
search.send_keys(Keys.RETURN)
button1 = tk.Button(text='Click Me',command=hello, bg='brown',fg='white')
canvas1.create_window(150, 150, window=button1)
root.mainloop()
CodePudding user response:
Instead of passing the value of the key executable_path
you may like to send both the key and the value as follows:
driver = webdriver.Chrome(executable_path=r'C:\Program Files (x86)\chromedriver.exe')
CodePudding user response:
thanks to @undetected Selenium
but with little change:
def hello ():
global driver
driver = webdriver.Chrome(executable_path=r'C:\Program Files (x86)\chromedriver.exe')
driver.get("https://google.com")
search = driver.find_element_by_xpath("//input[@type='text']")
search.send_keys('hello')
search.send_keys(Keys.RETURN)
you have to pass driver as a global variable and then send both the key and the value as :
driver = webdriver.Chrome(executable_path=r'C:\Program Files (x86)\chromedriver.exe')