As we know, the use of selenium usually do web automation, when running the code will open a browser to access the drive interface. Is there any way can be achieved without interface runs, let us in when debugging code can be shielded interface, to do other things, the answer is yes!
Plus it has been operating, the interface can be achieved without running.
Pilot Package:
from selenium.webdriver.chrome.options import Options
Add the following configuration:
chrome_options = Options()
chrome_options.add_argument ( '- window-size = 1920,1080') # size setting window interface
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=chrome_options)
Reference Code:
from selenium import webdriver import time import multiprocessing from selenium.webdriver.chrome.options import Options class Zutuan (): DEF __init__ (Self): "" " Open browser " "" self.chrome_options = Options () self.chrome_options.add_argument('--window-size=1920,1080') self.chrome_options.add_argument('--headless') self.driver = webdriver.Chrome(chrome_options=self.chrome_options) DEF open_zutuan (Self, url): "" " incoming tour url " "" self.driver.get(url) # Self.driver.maximize_window () self.driver.refresh () # the time.sleep (0.01) self.driver.implicitly_wait (30) # TODO implicitly implicit wait, wait element is visible def option_element(self, user, password): """xpath定位元素""" self.driver.find_element_by_xpath('//div[@class="login a"]/i').click() time.sleep(0.01) self.driver.find_element_by_xpath('//div[@class="a-title"]').click() self.driver.find_element_by_xpath('//input[@type="text" or @class="userName"]').send_keys(user) self.driver.find_element_by_xpath('//input[@type="password"]').send_keys(password) self.driver.find_element_by_xpath('//div[@class="button"]').click() time.sleep(1) self.driver.refresh() DEF select_commodity (Self, Content): "" " Search product group " "" # the TODO self.content instance attributes to the following method, if you want to use the value of the following methods to add the instance attribute solution self.content = Content self.driver.find_element_by_xpath('//input[@type="text"]').send_keys(content) self.driver.find_element_by_xpath('//div[@class="search"]').click() self.driver.refresh() #return content DEF the Result (Self): "" " judgment message after the successful search of goods, asserts the page is successful " "" IF self.content in self.driver.page_source: # Print (self.content) Print ( ' product search is successful, testing by ' ) the else : Print ( ' product search error, the test fails ' ) DEF Closed (Self): "" " Close the browser " "" the time.sleep ( 1 ) self.driver.quit() DEF runl (): # the TODO an operation sequence of the calling method performed ZT = Zutuan () zt.open_zutuan('http://www.zutuan.cn/index.html#/') zt.option_element('[email protected]', 'mg123456') zt.select_commodity ( ' banana ' ) zt.result() zt.closed() class View_details (Zutuan): "" " to add items to the star of a single product, " "" DEF check_commodity (Self, Number The): "" " to enter the product details page, click Add a star of a single product " "" self.driver.find_element_by_xpath ( ' // A [@target = "_ blank"] / IMG ' ) .click () self.driver.switch_to.window(self.driver.window_handles[1]) self.driver.find_element_by_xpath('//div[@class="child start"]').click() self.driver.find_element_by_xpath('//div[@class="el-dialog__body"]//input[@type="text"]').send_keys(number) self.driver.find_element_by_xpath('//button[@type="button" and @class="el-button el-button--danger"]').click() time.sleep(1) DEF the Result (Self): "" " override the parent class method, determine message after successfully added goods, whether asserted page success " "" IF ' added successfully ' in self.driver.page_source: Print ( ' commodity added successfully, testing by ' ) the else : Print ( ' items to add fails the test fails ' ) # call the parent class method to close the super () closed (). def run2(): vd = View_details() vd.open_zutuan('http://www.zutuan.cn/index.html#/') vd.option_element('[email protected]', 'mg123456') vd.select_commodity ( ' Apple ' ) vd.check_commodity(91628) vd.result () def main(): p1 = multiprocessing.Process(target=run1) p2 = multiprocessing.Process(target=run2) p1.start() p2.start() if __name__ == '__main__': main()