Accessing local HTML
# key: get (file:// + HTML path) in file:// cannot little self. The driver. The get (" file:////Users/admin/PycharmProjects/Test/Testcase/forms.html ")Copy the code

The complete Code:

HTML
<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="javascript:alert('Hello')"> Username: <input type="text" name="username" id="username"><br> Password: <input type="password" name="pwd" id="pwd"><br> <input type="submit" value="submit" id="submit"> </form> </body> </html>Copy the code
Python
from selenium import webdriver from time import sleep import os class TestCase(object): def __init__(self): Self.driver = webdriver.chrome () # path = os.path.abspath(__file__) # Current file path = Os.path. dirname(os.path.abspath(__file__)) # file file path file_path = 'file:///' + path + '/forms.html' Print (file_path) # HTML self.driver.get(file_path) def test_login(self): Username = self.driver.find_element_by_id('username') username. Send_keys ('Admin') # self.driver.find_element_by_id('pwd') pwd.send_keys('Test! Print (username. Get_attribute ('value')) print(pwd.get_attribute('value')) sleep(2) Self.driver.find_element_by_id ('submit').click() # confirm and turn off alert self.driver.switch_to.alert username.clear() pwd.clear() sleep(2) self.driver.quit() if __name__ == '__main__': case = TestCase() case.test_login()Copy the code