繁体   English   中英

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

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

嗨,我从tkinter GUI使用selenium webdriver打开一个网站。当我输入url时,我在gui中有一个条目和一个按钮,然后按下该按钮,网络浏览器现在开始打开。我想添加另一个功能,如果我使用鼠标光标在该网页上选择任何文本,然后再次在GUI上按另一个按钮,它将在tkinter GUI的其他输入区域中显示所选文本。 因此,是否可以添加此功能。 这是我的代码:-

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) 

根本原因是您的网站没有方法getSelectionText() 您可以添加然后调用它,请参见下面的代码:

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