如何使用Selenium访问浏览器的原生搜索按钮?
我在用Python的Selenium库搜索一个网址:https://rewards.bing.com/pointsbreakdown
,于是写了下面这段代码。
search_in_first = driver.find_element(By.ID, 'sb_form_q')
search_in_first.send_keys(search_random_word())
search_in_first.submit()
但是它却把我重定向到了一个新的谷歌/必应搜索网页,这是错误的,而不是我想要的正确的页面。所以我想知道怎么在我的Python脚本中找到内置的搜索框。
我尝试了下面这段代码
search_in_first = driver.find_element(By.ID, 'sb_form_q')
search_in_first.send_keys(search_random_word())
search_in_first.submit()
想要找到搜索框,但效果不太好,无法在这个框里进行搜索。
内层搜索框:
而不是这个:
4 个回答
0
如果你想打开一个链接,你需要使用 driver get(url)
这个命令。
0
看起来你想在微软Edge浏览器的搜索框里输入网址来直接访问,这样是行不通的。你在必应搜索框里输入的网址会变成搜索结果。
正确的访问网址的方法是使用 driver.get()
来打开网址。
0
给你,这个适用于微软Edge浏览器哦。
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from selenium.webdriver.edge.options import Options
import time
options = Options()
driver_service = Service(EdgeChromiumDriverManager().install())
driver = webdriver.Edge(service=driver_service, options=options)
# Define the search keyword
keyword = "hello"
driver.get(f"https://www.bing.com/search?q={keyword}")
time.sleep(10) # Wait for 10 seconds