Elements that are not visible on the Web page are not displayed on the page, but exist in the DOM tree, and these elements can be found webdriver.
The element.is_displayed () method can determine whether an element is displayed on the page, or False if the display returns TRUE.
HTML code for testing:
<HTML> <Head> <title>Displaying and hiding elements in HTML</title> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><Scripttype= "Text/javascript"> functionShowAndHidden1 () {varDiv1=document.getElementById ("Div1"); varDiv2=document.getElementById ("Div2"); if(Div1.style.display=='Block') Div1.style.display='None'; ElseDiv1.style.display='Block'; if(Div2.style.display=='Block') Div2.style.display='None'; ElseDiv2.style.display='Block'; } functionShowAndHidden2 () {varDiv3=document.getElementById ("Div3"); vardiv4=document.getElementById ("div4"); if(div3.style.visibility=='Visible') div3.style.visibility='Hidden'; Elsediv3.style.visibility='Visible'; if(div4.style.visibility=='Visible') div4.style.visibility='Hidden'; Elsediv4.style.visibility='Visible'; } </Script> </Head> <Body> <Div>Display: element does not occupy page position</Div> <DivID= "Div1"style= "Display:block;">DIV 1</Div> <DivID= "Div2"style= "Display:none;">DIV 2</Div> <inputID= "Button1"type= "button"onclick= "showAndHidden1 ();"value= "div Toggle" /> <HR> <Div>Visibility: element occupies page position</Div> <DivID= "Div3"style= "visibility:visible;">DIV 3</Div> <DivID= "Div4"style= "Visibility:hidden;">DIV 4</Div> <inputID= "Button2"type= "button"onclick= "showAndHidden2 ();"value= "div Toggle" /> </Body> </HTML>
Code to invoke the API
#Encoding=utf-8ImportUnitTestImport Time fromSeleniumImportWebdriverclassVisiturl (unittest. TestCase):defsetUp (self): Self.driver= Webdriver. Chrome (executable_path="E:\\chromedriver") deftest_getwebelementisdisplayed (self):#accessing custom HTML pagesSelf.driver.get ("http://127.0.0.1/test_visible.html") #find a second div element by id= "Div2"Div2 = self.driver.find_element_by_id ("Div2") #determines whether a second div element is visible on the page Printdiv2.is_displayed ()#Click the first Toggle div button to display a second div on the pageSELF.DRIVER.FIND_ELEMENT_BY_ID ("button1"). Click ()#again to determine if the second div element is visible Printdiv2.is_displayed ()#Fourth div element found by id= "Div4"Div4 = self.driver.find_element_by_id ("div4") #determines whether a fourth div element is visible on the page Printdiv4.is_displayed ()defTearDown (self): Self.driver.quit ()if __name__=='__main__': Unittest.main ()
Selenium Common API (vii) determine if an element is visible