【问题标题】:How to run Selenium within Kivy (browser opens in Kivy window)如何在 Kivy 中运行 Selenium(浏览器在 Kivy 窗口中打开)
【发布时间】:2020-07-13 22:51:33
【问题描述】:

我编写了一个小脚本,用户可以在其中输入产品编号,然后用它来查找商品。我正在使用 chrome 的 webdriver。它现在按预期工作。

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()

但是,我想在 android 上将其作为应用程序运行(这就是我在 Kivy 中编写它的原因)。我试图查找是否有任何适用于 android webbrowser 的 webdriver,或者我在这里使用的 webdriver 是否适用于移动版 Chrome,但我找不到任何东西。

此外,我希望浏览器在 Kivy 窗口中打开(即在应用程序窗口中显示浏览器),而不是单独打开浏览器。就目前而言,chrome 浏览器是单独打开的。

任何帮助将不胜感激!

【问题讨论】:

    标签: python selenium selenium-webdriver kivy


    【解决方案1】:

    我不相信这是你会用硒做的事情。我相信你会想要使用移动操作系统的原生浏览器。

    关于如何做到这一点还有另一个堆栈溢出问题here

    【讨论】:

    • 这让我陷入了地狱般的困境,直到我意识到你不能在 Windows 上编写 android 应用程序(或者至少你不能测试它们,这使得它无论如何都没用)。希望他们能把它放在他们的网页上,到目前为止不会浪费我的时间
    猜你喜欢
    • 1970-01-01
    • 2019-03-27
    • 2014-09-04
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多