简体   繁体   中英

why it is required to open google home page from chrome browser to enter search from selenium

I had a doubt with my code . Why is it required to open a google webpage to search something in selenium .why can't we search directly search from chrome browser. below is my code

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
chrome_options=webdriver.ChromeOptions()
chrome_options.add_argument("--disable-infobars")
browser=webdriver.Chrome('C:/Users/chromedriver.exe',chrome_options=chrome_options)
page=browser.get('http://www.google.com')
search=browser.find_element_by_name('q')
search.send_keys('aditya')
search.send_keys(Keys.RETURN)
time.sleep(5)

why is the line page=browser.get(' http://www.google.com ') required. I am anyways using browser to send keys why is page required?

When you first instantiate a new browser driver, it is just a blank browser. You need to navigate to a URL before you can interact with any objects on that page. In your case, you are trying to do a google search, so you must navigate to the page first before being able to find the search element.

You shouldn't have to assign it to the page variable though. You should be able to just write:

browser.get('http://www.google.com')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM