简体   繁体   中英

How to run Selenium within Kivy (browser opens in Kivy window)

I've written a small script where the user inputs a product number, that is then used to look up an item. I'm using the webdriver for chrome. It works as intended right now.

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.image import Image
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class KivyButton(App):
    def update(self,instance,*args):
        driver = webdriver.Chrome()
        driver.get('https://us.pandora.net/')
        fill_box = driver.find_element_by_xpath('//*[@id="q"]')
        fill_box.clear()
        fill_box.send_keys(self.product_number.text)
        try:
            WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[1]/div/div[1]/header/nav/div[2]/div[3]/div/div/div/div[2]/div/a/div[1]/img'))).click()
        except:
            print('invalid info')
            driver.close()

    def build(self):
        self.layout  = FloatLayout(size=(300,300))
        image=Image(source='pandora.jpg',allow_stretch=True,keep_ratio=False)
        title=Label(text='Please Enter Product Number',color=[0,0,0,1],font_size='20dp', pos=(200,350),size_hint=(0.1,.1))
        self.mybtn=Button(text='Enter',on_press=self.update,pos=(500,300),size_hint=(.1,.1))
        self.product_number=TextInput(text='',font_size='40dp',pos=(100,300),size_hint=(.5,.1))
        self.layout.add_widget(image)
        self.layout.add_widget(self.product_number)
        self.layout.add_widget(self.mybtn)
        self.layout.add_widget(title)
        return self.layout

KivyButton().run()

However, I want to run this as an app on android (which is why I've written it in Kivy). I've tried to look up whether there is any webdriver for the android webbrowser, or if the webdriver I'm using here will work with the mobile version of Chrome, but I haven't been able to find anything.

Additionally, rather than open up the browser separately, I'd like the browser to open up within the Kivy window (ie display the browser within the app window). As it stands, the chrome browser opens up separately.

Any help would greatly be appreciated!

I don't believe that is something you would do with selenium. I believe you would want to use the Native browser of the Mobile OS.

There is another stack overflow question regarding how to do that here

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