简体   繁体   中英

I want to use Selenium's send keys() via a textInput variable defined in the build function of kivy framework in python

 def build(self):
    screen = Screen()
    type = TextInput(text="Enter job domain",pos = (270,380),size_hint =(.3, .1))
    btn = Button(text="Job Search",size_hint =(.2, .2), pos =(300, 250),background_color =(0, 0, 1, 1),)
    btn.bind(on_press=self.callback)
    screen.add_widget(type)
    screen.add_widget(btn)
    return screen

 def callback(self, event):
     driver = webdriver.Chrome(ChromeDriverManager().install())
     driver.get('https://indeed.com')
     search_job = driver.find_element_by_xpath('//*[@id="text-input-what"]')
     search_job.send_keys(['data science'])
     initial_search_button = driver.find_element_by_xpath("//*[@id='whatWhereFormId']/div[3]/button")
     initial_search_button.click()

Here I want to use the text from the type variable initialized with a textInput variable in the def build(self) function and use it in the def callback(self,event) function in the search_job.send_keys(['']) part

If you want to use the text from a TextInput , just save a reference to that TextInput (perhaps call it self.type ):

 def build(self):
    screen = Screen()
    self.type = TextInput(text="Enter job domain",pos = (270,380),size_hint =(.3, .1))
    btn = Button(text="Job Search",size_hint =(.2, .2), pos =(300, 250),background_color =(0, 0, 1, 1),)
    btn.bind(on_press=self.callback)
    screen.add_widget(self.type)
    screen.add_widget(btn)
    return screen

Then use that reference wherever you need it:

 def callback(self, event):
     driver = webdriver.Chrome(ChromeDriverManager().install())
     driver.get('https://indeed.com')
     search_job = driver.find_element_by_xpath('//*[@id="text-input-what"]')
     search_job.send_keys([self.type.text])
     initial_search_button = driver.find_element_by_xpath("//*[@id='whatWhereFormId']/div[3]/button")
     initial_search_button.click()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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