To start testing, you need to know what browser to test first. How to install the driver for the browser, have talked before.
The second is to know how to open the browser, this, in the previous code, also reflected, but did not delve into. Let's talk about the browser operation.
fromSeleniumImportWebdriver fromTimeImportSleep#Open BrowserDriver =Webdriver. Firefox ()#Open Web pageDriver.get ("https://www.baidu.com/")#set the current browser width height to 200,300Driver.set_window_size (" $"," -") Sleep (2)#Browser MaximizationDriver.maximize_window ()#Print Browser titlePrint(Driver.title)#print the current URLPrint(Driver.current_url)#Browser forward backDriver.find_element_by_name ("tj_trnews"). Click () Driver.back () Sleep (2) Driver.forward () Sleep (2)#Exit BrowserDriver.quit ()
fromSeleniumImportWebdriverImport Time#close a browser windowDriver =Webdriver. Firefox () Driver.get ("http://home.firefoxchina.cn/") Driver.find_element_by_css_selector ("a[title= ' Micro blog '"). Click ()#switch to the window you want to closeDriver.switch_to.window (driver.window_handles[-1])#Close the current windowDriver.close ()
The problem of switching Windows is introduced in the Selenium switch window
Selenium browser-based operations (Python)