首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenium-Python不接受来自Tkinter入口Widget的输入--TypeError:类型为'function‘的对象没有len()

Selenium-Python不接受来自Tkinter入口Widget的输入--TypeError:类型为'function‘的对象没有len()
EN

Stack Overflow用户
提问于 2020-08-19 00:35:48
回答 1查看 139关注 0票数 1

大家晚上好,

希望你做得很好。我有个问题,我希望你能给我一些启示。

错误:

代码语言:javascript
复制
Exception in Tkinter callback

Traceback (most recent call last):

File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)

File "", line 69, in <lambda> self.mini_refresh.config(command=lambda: LaunchSiteFunction._launch_mini(self))

File "", line 42, in _launch_mini
    self.mac_mini_search_bar.send_keys(lambda:TmobileGUI._mac_mini_name_entry(self))

File "C:\Users\\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webelement.py", line 478, in send_keys
    {'text': "".join(keys_to_typing(value)),

File "C:\Users\\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\common\utils.py", line 150, in keys_to_typing
    for i in range(len(val)):
 
TypeError: object of type 'function' has no len()

问题:

此问题发生在将Tkinter代码和Selenium代码更改为类之后。(我对python很陌生,但我正在努力熟悉类)。

以前:

在"mini_entry"

  • Hit刷新中输入
  • I,Selenium将启动一个网站
  • ,从"mini_entry“获取输入(例如: W85FGSD9385)
  • Input,即进入网站搜索栏
  • ,该网站将返回关于W85FGSD9385

的任何信息。

后:

我可以在"mini_entry"

  • Hit刷新中键入一些内容,但Selenium现在启动website

  • Doesn't,做任何事情。

提前感谢!

代码语言:javascript
复制
class LaunchSiteFunction:

    def _launch_mini(self):

        self.mini_site = webdriver.Chrome()
        self.mini_site.get('https://jamf-tools.apps.px-npe01.cf.t-mobile.com/mini_health/')
        self.mini_site.implicitly_wait(10)
        self.mac_mini_search_bar = self.mini_site.find_element_by_id('myInput')
        self.mac_mini_search_bar.clear()                                         
        self.mac_mini_search_bar.send_keys(lambda: GUI._mac_mini_name_entry(self)))
        self.mac_mini_search_bar.send_keys(Keys.RETURN)

class GUI:

    def run_program(self):
        root.title("Network GUI") 
        self._mac_mini_name_label()
        self._mac_mini_name_entry()
        self._mac_mini_refresh_button()
        self._mac_mini_output_label()

    # Lines mac mini section:
    def _mac_mini_name_label(self): 
        self.mini_label = tk.Label(program_frame, text='Mac Mini', bg='#ffffff', fg='#ea0a8e', font=('Calibre', 10))
        self.mini_label.place(relwidth=0.11, relheight=.1)

    def _mac_mini_name_entry(self):
        self.mini_entry = tk.Entry(program_frame, bg='#000000', fg='#ffffff', font=('Calibre', 10))
        self.mini_entry.place(relx=0.12, relheight=.1, relwidth=0.18)

    def _mac_mini_refresh_button(self):
        self.mini_refresh = tk.Button(program_frame, text='Refresh', bg='#000000', fg='#ea0a8e', font=('Calibre', 10))
        self.mini_refresh.place(relx=0.31, relheight=.1, relwidth=0.1)
        self.mini_refresh.config(command=lambda: LaunchSiteFunction._launch_mini(self))

    def _mac_mini_output_label(self):
        self.mini_output = tk.Label(program_frame, bg='#ffffff', bd='10')
        self.mini_output.place(relx=0.42, relheight=.1, relwidth=0.15)

if "__main__" == __name__:
run = GUI()
run.run_program()
root.mainloop()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-19 02:06:06

我没有selenium来测试这个,但是只要您的launch site总体逻辑是正确的,它就会修复它。我所做的就是重新格式化你所写的大部分内容,并修改了2部分。

第一部分

这是错误的:lambda: GUI._mac_mini_name_entry(self)

这是对的:self.mini_entry.get()

第二部分

所有这些都是错误或奇怪的。

代码语言:javascript
复制
if "__main__" == __name__:
run = GUI()
run.run_program()
root.mainloop()

在我给您的示例中,Approot。它扩展了root,因此,您对它所做的一切都是直接对root进行的。话虽如此,初始化变得更干净了。

代码语言:javascript
复制
if "__main__" == __name__:
    app = App()
    app.title("Network GUI")
    app.mainloop()

您必须添加您的selenium导入。你没发,我也猜不出来。您将注意到,您的所有方法都不见了,您的LaunchSiteFunction类已经转换为root(App)上的方法。通过这种方式,您不必处理大量的远程数据。一切都是self的一部分,所有东西都可以访问self。您没有做任何符合新类条件的操作(比如扩展webdriver之类的),所以这一切都是在App中进行的。

代码语言:javascript
复制
import tkinter as tk


class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        
        self.mini_label = tk.Label(self, text='Mac Mini', bg='#ffffff', fg='#ea0a8e', font=('Calibre', 10))
        self.mini_label.place(relwidth=0.11, relheight=.1)
        
        self.mini_entry = tk.Entry(self, bg='#000000', fg='#ffffff', font=('Calibre', 10))
        self.mini_entry.place(relx=0.12, relheight=.1, relwidth=0.18)
        
        self.mini_refresh = tk.Button(self, text='Refresh', bg='#000000', fg='#ea0a8e', font=('Calibre', 10))
        self.mini_refresh.place(relx=0.31, relheight=.1, relwidth=0.1)
        self.mini_refresh.config(command=self.launch_site)
        
        self.mini_output = tk.Label(self, bg='#ffffff', bd='10')
        self.mini_output.place(relx=0.42, relheight=.1, relwidth=0.15)
        
    def launch_site(self):
        self.mini_site = webdriver.Chrome()
        self.mini_site.get('https://jamf-tools.apps.px-npe01.cf.t-mobile.com/mini_health/')
        self.mini_site.implicitly_wait(10)
        self.mac_mini_search_bar = self.mini_site.find_element_by_id('myInput')
        self.mac_mini_search_bar.clear()                                         
        self.mac_mini_search_bar.send_keys(self.mini_entry.get())
        self.mac_mini_search_bar.send_keys(Keys.RETURN)


if "__main__" == __name__:
    app = App()
    app.title("Network GUI")
    app.mainloop()

ProTip:

了解如何使用pack()grid()。你对所有的place()都没什么好处。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63478460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档