How to access the native search button of a browser using Selenium?

70 Views Asked by At

I was trying to search for a url: https://rewards.bing.com/pointsbreakdown with the help of Selenium in Python so I wrote the following block of code

search_in_first = driver.find_element(By.ID, 'sb_form_q')
search_in_first.send_keys(search_random_word())
search_in_first.submit()

but it redirects to a new google/bing search webpage Wrong insted of Right. So I want to know how to get hold of the inbuilt search box in my python script.

I tried the following code block

search_in_first = driver.find_element(By.ID, 'sb_form_q')
search_in_first.send_keys(search_random_word())
search_in_first.submit()

to get hold of the search box but it doesn't work well and search in this box.

Inner search box:

Inner search box

instead of this:

Outer search box

4

There are 4 best solutions below

0
Danya Ganashok On

If you want to follow the link, then you need to use. driver get(url)

1
Aariyan Patel On
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
import time 


options = Options() 
options.add_argument("user-data-dir=C:\\Users\\yourusername\\AppData\\Local\\Google\\Chrome Beta\\User Data")
options.add_argument("profile-directory=Default")
driver_service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(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
0
Aariyan Patel On

here you GO :) this work for Microsoft Edge browser

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
0
Kendrick Li On

It seems you are trying to use the search box for URL navigation in Microsoft Edge, which is not possible. URLs you put into the Bing search box will turn into search results.

The correct way for URL navigation should be using driver.get() for the URL.