如何使用python将字符串插入浏览器文本字段

时间:2018-07-15 23:46:15

标签: python

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)

1 个答案:

答案 0 :(得分:1)

old <- '<!DOCTYPE html> <h1>Hello</h1> <br> <p>I am an html file</p> <script myscript1 src="https://website.com/javascripts.js" type="text/javascript"></script> <p>I am a paragraph</p> <script myscript2 src="https://website2.com/function.js" type="text/javascript"></script>' write(old,"old.html") library(xml2) doc <- read_xml("old.html", as_html=TRUE) write_xml(doc, "foo.html") bar <- readLines("foo.html") barbar <- bar[!grepl("myscript1",bar)] barbar #[1] "<!DOCTYPE html>" #[2] "<html><body>" #[3] "<h1>Hello</h1>" #[4] "<br><p>I am an html file</p>" #[5] "<script myscript2 src=\"https://website2.com/function.js\" # type=\"text/javascript\"></script>" #[6] "</body></html>"rite(barbar, "new.html") 模块不能为您提供对浏览器的太多控制。解决直接问题的最直接,最常见的方法是使用selenium library

webbrowser

请确保遵循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") 安装步骤并安装了chromedriver,以便能够通过selenium控制Chrome浏览器。

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