【问题标题】:How to insert a string into a browser text field using python如何使用python将字符串插入浏览器文本字段
【发布时间】:2018-12-23 11:35:48
【问题描述】:

Google Search Box Text Field

使用 Python 将字符串放入文本字​​段的最快方法是什么。假设可以从 excel 文件中解析字符串

import webbrowser  
url = 'http://google.com'  
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
webbrowser.get(chrome_path).open(url)

from pynput.keyboard import Key, Controller keyboard = Controller()

import xlrd


workbook = xlrd.open_workbook("Book1.xlsx") sheet =
workbook.sheet_by_index(0)

print (sheet.cell(0,0).value)

【问题讨论】:

  • 您需要与 Chrome 的 UI 进行物理交互,还是只需要处理来自 google 搜索的响应? webbrowser 似乎用于将用户重定向到他们浏览器中的网站,如果你不需要这个,你最好使用robobrowser 之类的东西。

标签: python


【解决方案1】:

webbrowser 模块无法让您对浏览器进行太多控制。解决当前问题的最直接和最常用的方法是使用selenium library

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://google.com")

search_input_box = driver.find_element_by_name("q")
search_input_box.send_keys("test search")

请务必按照selenium的安装步骤安装chromedriver,以便能够通过selenium控制Chrome浏览器。

请注意,这是对您问题的直接回答。您实际上不需要自动化常规浏览器来发出 google 搜索请求:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2021-02-26
    • 2015-06-27
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多