from selenium import webdriver
from selenium.webdriver.common.by import By
#不自动关闭网页
chrome_option = webdriver.ChromeOptions()
chrome_option.add_experimental_option('datach',True)
#创建自动化对象
text2 = webdriver.Chrome(options=chrome_option)
#使用自动化对象打开网页,使用get方法获得网址
text2.get("https://www.baidu.com/")
#创建一个对象定位需要输入的文本框
text_search = text2.find_element(By.ID,'search-input')
#输入想要搜索的文本,'\n'相当于点击搜索
text_search.send_keys('hello\n')
请问为什么这里运行不了
这得看他具体报什么错了,你是想自动化打开网站,但是https请求是需要安装相关插件的,如果你是centos环境,可以尝试先安装相应的插件,再执行该脚本试试。
希望可以帮到你
源代码有些问题,如下图:
同时,text2.find_element(By.ID,'search-input')改为text2.find_element(By.ID,'kw')
完整代码如下:
from selenium import webdriver
from selenium.webdriver.common.by import By
#不自动关闭网页
chrome_option = webdriver.ChromeOptions()
chrome_option.add_experimental_option('detach',True)
#创建自动化对象
text2 = webdriver.Chrome(options=chrome_option)
#使用自动化对象打开网页,使用get方法获得网址
text2.get("https://www.baidu.com/")
#创建一个对象定位需要输入的文本框
text_search = text2.find_element(By.ID,'kw')
#输入想要搜索的文本,'\n'相当于点击搜索
text_search.send_keys('hello\n')
如果有用,请采纳,谢谢~
代码中有一个语法错误,第6行的代码
chrome_option.add_experimental_option('datach',True)
应该是
chrome_option.add_experimental_option('detach',True)。
将错误更正后,完整的代码如下所示:
from selenium import webdriver
from selenium.webdriver.common.by import By
#不自动关闭网页
chrome_option = webdriver.ChromeOptions()
chrome_option.add_experimental_option('detach',True)
#创建自动化对象
text2 = webdriver.Chrome(options=chrome_option)
#使用自动化对象打开网页,使用get方法获得网址
text2.get("https://www.baidu.com/")
#创建一个对象定位需要输入的文本框
text_search = text2.find_element(By.ID,'search-input')
#输入想要搜索的文本,'\n'相当于点击搜索
text_search.send_keys('hello\n')