Home>

I want to make a program that searches the input protein name using tkinter and selenium in python, but first I want to input it in EditBox and get the result and search it in the browser opened by search_id.send_keys (EditBox_value) But I can't. What's wrong?

import tkinter
from selenium import webdriver
from time import sleep
import requests
from bs4 import BeautifulSoup

root = tkinter.Tk ()
root.title (u "protein search")
root.geometry ("500x500")

label

Static1 = tkinter.Label (text = u'protein')
Static1.pack ()

EditBox = tkinter.Entry (width = 50)
EditBox.insert (tkinter.END, "")
EditBox.place (x = 170, y = 100)
EditBox.pack ()
EditBox_value = EditBox.get ()
btn = tkinter.Button (root, text ='Search for proteins', command = btn_click)
btn.place (x = 170, y = 200)

Run button

def btn_click ():
browser = webdriver.Chrome ('chromedriver.exe')
browser.get ("https://www.rcsb.org/")
search_id = browser.find_element_by_id ("search-bar-input-text")
search_id.send_keys (EditBox_value)

Error message

Exception in Tkinter callback
Traceback (most recent call last):
File "C: \ ANACONDA \ lib \ tkinter \init.py ", line 1705, incall
return self.func (* args)
File "", line 32, in btn_click
search_id1 = browser.find_element_by_id ("menu1")
File "C: \ ANACONDA \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py", line 360, in find_element_by_id
return self.find_element (by = By.ID, value = id_)
File "C: \ ANACONDA \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py", line 978, in find_element
'value': value}) ['value']
File "C: \ ANACONDA \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py", line 321, in execute
self.error_handler.check_response (response)
File "C: \ ANACONDA \ lib \ site-packages \ selenium \ webdriver \ remote \ errorhandler.py", line 242, in check_response
raise exception_class (message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method": "css selector", "selector": "[id =" menu1 "]"}
(Session info: chrome = 86.0.4240.198)

  • Answer # 1

    At present, immediately after the widget is displayed, the value is acquired when nothing is entered.
    Let's get the value of entry in the function that fires when the button is clicked.

    def btn_click ():
        EditBox_value = EditBox.get ()
        browser = webdriver.Chrome ('chromedriver.exe')
        browser.get ("https://www.rcsb.org/")
        search_id = browser.find_element_by_id ("search-bar-input-text")
        search_id.send_keys (EditBox_value)