最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Selenium中给百度搜索框中输入文字并触发搜索

搜索 crifan 3720浏览 0评论
折腾:
【未解决】Mac中用Selenium自动操作浏览器实现百度搜索
期间,此处希望实现Selenium中,对于已经定位到的百度首页的搜索框,输入文字,并触发搜索
Selenium 输入文字
selenium input text
Selenium Form WebElement: TextBox, Button, sendkeys(), click()
java – Selenium Webdriver: Entering text into text field – Stack Overflow
How to locate and insert a value in a text box (input) using Python Selenium? – Stack Overflow
selenium input text python
Selenium Form WebElement: TextBox, Button, sendkeys(), click()
How to locate and insert a value in a text box (input) using Python Selenium? – Stack Overflow
java – Using Selenium Web Driver to retrieve value of a HTML input – Stack Overflow
Selenium WebDriver get text from input field – Stack Overflow
How to use Selenium to input text in Python – Stack Overflow
Get value of an input box using Selenium (Python) – Stack Overflow
3. Navigating — Selenium Python Bindings 2 documentation
此处,去输入内容,用代码:
searchButtonElem.send_keys("crifan")
在输入之前,最好清除已有内容:
searchButtonElem.clear()
效果:
之后,对于:触发搜索
正常的思路是:
点击 百度一下 这个按钮
不过此处还有另外一个思路:
键盘上按 Enter回车键,也可以
所以分别去试试:
# emulate press Enter key
searchButtonElem.send_keys(Keys.RETURN)
效果:
是可以:搜索出结果的。
另外,再去换另外一种:
找到 百度一下 按钮
并点击 百度一下 按钮
【整理】用Chrome或Chromium查看百度首页中各元素的html源码
然后用代码去定位了:
BaiduSearchId = "su"
baiduSearchButtonElem = chromeDriver.find_element_by_id(BaiduSearchId)
print("baiduSearchButtonElem=%s" % baiduSearchButtonElem)
然后再去搞清楚,如何模拟点击按钮
直接去官网教程中找:
3. Navigating — Selenium Python Bindings 2 documentation
element = driver.find_element_by_xpath("//select[@name='name']")
all_options = element.find_elements_by_tag_name("option")

    print("Value is: %s" % option.get_attribute("value"))
    option.click()
直接就是click了。
所以用第二套方法,找到按钮并点击:
代码:
# Method 2: find button and click
BaiduSearchId = "su"
baiduSearchButtonElem = chromeDriver.find_element_by_id(BaiduSearchId)
print("baiduSearchButtonElem=%s" % baiduSearchButtonElem)
baiduSearchButtonElem.click()
print("Clicked button %s" % baiduSearchButtonElem)
效果是一样的:
【总结】
Selenium中,对于已经定位到的百度首页的搜索框,去输入文字,并触发搜索的代码是:
注:可以先清除(如果之前有输入过内容的话)输入框中值:
searchButtonElem.clear()
先输入要搜索的文字:
searchStr = "crifan"
searchButtonElem.send_keys(searchStr)
再去触发搜索
此处由于页面支持直接回车搜索,所以可以用:
# Method 1: emulate press Enter key
searchButtonElem.send_keys(Keys.RETURN)
去模拟回车键,触发搜索
也可以,按照常规办法,找到对应按钮:
BaiduSearchId = "su"
baiduSearchButtonElem = chromeDriver.find_element_by_id(BaiduSearchId)
然后再去点击按钮去搜索:
baiduSearchButtonElem.click()
附上此段逻辑的完整代码:
searchStr = "crifan"
searchButtonElem.send_keys(searchStr)
print("Entered %s to search box" % searchStr)

# click button
# Method 1: emulate press Enter key
# searchButtonElem.send_keys(Keys.RETURN)
# print("Pressed Enter/Return key")

# Method 2: find button and click
BaiduSearchId = "su"
baiduSearchButtonElem = chromeDriver.find_element_by_id(BaiduSearchId)
print("baiduSearchButtonElem=%s" % baiduSearchButtonElem)
baiduSearchButtonElem.click()
print("Clicked button %s" % baiduSearchButtonElem)
即可。

转载请注明:在路上 » 【已解决】Selenium中给百度搜索框中输入文字并触发搜索

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
80 queries in 0.178 seconds, using 22.04MB memory