python - 如何在 Kivy 中运行 Selenium(浏览器在 Kivy 窗口中打开)

标签 python selenium selenium-webdriver kivy

我编写了一个小脚本,用户在其中输入产品编号,然后用于查找商品。我正在使用 chrome 的网络驱动程序。它现在按预期工作。

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 网络浏览器的网络驱动程序,或者我在这里使用的网络驱动程序是否适用于移动版 Chrome,但我没有找到任何内容。

此外,我希望浏览器在 Kivy 窗口中打开(即在应用程序窗口中显示浏览器),而不是单独打开浏览器。按照目前的情况,Chrome 浏览器会单独打开。

任何帮助将不胜感激!

最佳答案

我不相信你会用 Selenium 来做这件事。我相信您会想使用移动操作系统的 native 浏览器。

还有另一个关于如何执行此操作的堆栈溢出问题 here

关于python - 如何在 Kivy 中运行 Selenium(浏览器在 Kivy 窗口中打开),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62885508/

相关文章:

Python selenium 'list' 对象没有属性 'text' 错误

python - 使用 python/selenium 从元素链接中提取部分文本

javascript - 为什么 WebdriverIO 对从未使用过的 webelement 发出 POST 'element' 请求?

testing - Robotframework:如何在打开浏览器的情况下在 Selenium 库中使用 Chromium?

python - MatPlotLib Python - 来自颜色传感器的 RGB 值用于更改线点的颜色

python - 检查当前时间是否在 python 列表中可用

python - 我会用什么样的正则表达式来匹配这个?

python - pip3 停止将可执行文件安装到/usr/local/bin 中

python - 当所有输入的功能相同时如何获得按钮的唯一 XPATH

python - Selenium 网络驱动程序 : How do I find ALL of an element's attributes?