[英]Using same Selenium webdriver for multiple actions on website?
I am trying to create a GUI to modify a Javascript file on a website.我正在尝试创建一个 GUI 来修改网站上的 Javascript 文件。 It is used to create Leaflet markers without having to touch the code.
它用于创建 Leaflet 标记,而无需接触代码。
So my idea for the setup is that I first initiate the Webdriver and go to the respective JavaScript file in the first function.所以我对设置的想法是,我首先将 Webdriver 和 go 启动到第一个 function 中的相应 JavaScript 文件。
def start():
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome(executable_path="chromedriver")
browser.get(---> move to the javascript file on the website)
return browser
Then I want to have several other functions that can act on the textarea, like adding lines, deleting lines etc. The problem is that in the other functions I also have to use the "browser" variable from the start function:然后我想要几个可以作用于文本区域的其他函数,比如添加行、删除行等。问题是在其他函数中,我还必须从 function 开始使用“浏览器”变量:
def CreateMarker():
from selenium.webdriver.common.keys import Keys
code = code.get()
textarea = browser.find_element_by_class_name('textarea')
textarea.send_keys(sending some keys)
safe, yadada etc....
In the GUI (made with tkinter) I have one button bound to the start function to go the website and file and another button to add the marker code.在 GUI(使用 tkinter 制作)中,我有一个按钮绑定到开始 function 到 go 网站和文件以及另一个按钮来添加标记代码。
So the problem is, that the createmarker function doesnt recognize the browser variable and when I pass the variable in the CreateMarker function as CreateMarker(browser) it complains that "browser" is not defined, as soon as I want to run the GUI...所以问题是,createmarker function 无法识别浏览器变量,当我将 CreateMarker function 中的变量作为 CreateMarker(browser) 传递时,它会抱怨“浏览器”未定义,只要我想运行 GUI。 .
Okay so I finally got everything to work the way I want.好的,所以我终于让一切都按照我想要的方式工作了。 The cracking bit was just to make the webdriver a global variable and call that in the right way in the main function.
破解位只是使 webdriver 成为全局变量,并在主 function 中以正确的方式调用它。
So now I have two buttons on the main GUI that look like this:所以现在我在主 GUI 上有两个按钮,如下所示:
# Button to start the webdriver
Button(window, text = "Start", width=11, command=init.actions.start).grid(row=1, column=1, sticky=W)
# Textbox for what to write
Label (window, text='Enter text', bg="black", fg="white") .grid(row=2, column=0, sticky=W)
text = Entry(window, width=20, bg="white")
text.grid(row=3, column =0, sticky=W)
And I have a separate file in which I store all the functions connected to the buttons.我有一个单独的文件,其中存储了连接到按钮的所有功能。 The functions to start the webdriver and to write the text to the textarea looks like this:
启动 webdriver 并将文本写入 textarea 的函数如下所示:
class actions:
def start():
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
global browser
browser = webdriver.Chrome(executable_path="chromedriver")
browser.get("go to the login page")
login = browser.find_element_by_name("USER_LOGIN")
login.send_keys("USERNAME HERE")
pw= browser.find_element_by_name("USER_PASSWORD")
pw.send_keys("PASSWORD HERE")
pw.send_keys(Keys.ENTER)
time.sleep(5)
browser.get("page with the file")
return browser
def create_code(browser, text):
from selenium.webdriver.common.keys import Keys
Text=text.get()
textarea.send_keys(Text)
So now I have a button to initialze the webdriver and a separate one to write to the file.所以现在我有一个按钮来初始化 webdriver 和一个单独的按钮来写入文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.