简体   繁体   English

在python中使用Selenium执行Javascript(Tkinter作为GUI)

[英]executing Javascript using Selenium in python (Tkinter as GUI)

hi i am opening a website using selenium webdriver from a tkinter GUI.i have a entry and a button in gui when ever i give the url in entry and press the button the web browser starts opening now i want to add another functionality that if i select any text on that webpage using my mouse cursor and press another button again on GUI it should show the selected text in some other entry area in the tkinter GUI. 嗨,我从tkinter GUI使用selenium webdriver打开一个网站。当我输入url时,我在gui中有一个条目和一个按钮,然后按下该按钮,网络浏览器现在开始打开。我想添加另一个功能,如果我使用鼠标光标在该网页上选择任何文本,然后再次在GUI上按另一个按钮,它将在tkinter GUI的其他输入区域中显示所选文本。 So, Is it possible to add this functionality. 因此,是否可以添加此功能。 here is my code:- 这是我的代码:-

url1 = Entry(top, bd =3, width = 50)
url1.place(x=800 , y=100)

def open(url):
    driver = webdriver.Firefox()
    driver.set_window_size(600, 500)
    driver.set_window_position(300,300)
    driver.get(url)
    driver.implicitly_wait(20)
    driver.execute_script("text = getSelectionText(); alert(text)")

# submit button which is performing action on submit
submit1 = Button(top, text="Open", width=15, bg='lightblue', command=lambda: open(url1.get()))
submit1.place(x=1200, y=100) 

The root cause is your web site doesn't have method getSelectionText() . 根本原因是您的网站没有方法getSelectionText() You can add then call it, see code below: 您可以添加然后调用它,请参见下面的代码:

jsScript = '''
function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }
    return text;
}

text = getSelectionText(); alert(text)
'''

driver.execute_script(jsScript)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM