pythonselenium-webdrivermicrosoft-edgewebautomation

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


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


Solution

  • 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