Python+selenium Locating hidden elements

Source: Internet
Author: User

the principle of locating hidden features : The page is primarily "display:none" to control the invisible elements. So we need to modify the display's worth of display= "block to implement the element positioning via JavaScript.

HTML code:

"header">    <a href="#"class ="logo"><strong id="Title" style= " Display:none ">AAAA</strong></a>

The core code is as follows :

Driver =Webdriver. Chrome () Driver.maximize_window () Driver.get ('Http://127.0.0.1:5000/HimoMP') JS="document.getElementById (' Title '). style.display= ' Block '" #Writing JS StatementsDriver.execute_script (JS)#Execute JSContentText = driver.find_element_by_id ('Title'). Text#positioning ElementsPrint(ContentText)#Validation ResultsDriver.quit ()

Printing Results :

127.0.0.1--[16/nov/2017 19:14:56]"Get/himomp http/1.1"200-127.0.0.1--[16/nov/2017 19:14:56]"get/static/css/main.css http/1.1"200-127.0.0.1--[16/nov/2017 19:14:56]"get/static/js/jquery.min.js http/1.1"200-127.0.0.1--[16/nov/2017 19:14:56]"get/static/js/skel.min.js http/1.1"200-127.0.0.1--[16/nov/2017 19:14:56]"get/static/js/util.js http/1.1"200-127.0.0.1--[16/nov/2017 19:14:56]"get/static/js/main.js http/1.1"200-127.0.0.1--[16/nov/2017 19:14:56]"get/static/css/font-awesome.min.css http/1.1"200-127.0.0.1--[16/nov/2017 19:14:57]"get/static/fonts/fontawesome-webfont.woff2?v=4.6.3 http/1.1"200-127.0.0.1--[16/nov/2017 19:14:57]"Get/favicon.ico http/1.1"404-aaaa[finishedinch9.5S]

Sometimes we encounter elements that are not visible, and selenium cannot manipulate these elements at this time. For example, the following scenario:

Page mainly through "Display:none" to control the entire drop-down box is not visible. This time if you directly manipulate this drop-down box, you will be prompted:

 fromSeleniumImportWebdriver fromSelenium.webdriver.support.selectImportSelectImportOs,timedriver=Webdriver. Chrome () File_path='file:///'+ Os.path.abspath ('test.html') Driver.get (file_path) Sel= Driver.find_element_by_tag_name ('Select')--Find all the Tag_nameselect (SEL). Select_by_value ('Opel')--value= under All tag_nameOpeltime.sleep (2) Driver.quit ()
Exceptions. ElementNotVisibleException:Message:element not visible:element isn't currently visible and may isn't manipulated

We need to modify the display value through JavaScript.

' document.queryselectorall ("select") [0].style.display= "block"; '-- Find all the Select to cut out what you want, and then re-assign the display to Driver.execute_script (JS)--= driver.find_element _by_tag_name ('select') Select (SEL). Select_by_value ('Opel  ') ...

Document.queryselectorall ("select") [0].style.display= "block";

Document.queryselectorall ("select") selects all of the Select.

[0] Specify the first of the set of labels.

style.display= "Block"; Modify the style of the display= "block" to indicate that it is visible.

After the execution of this JS code, you can normally operate the drop-down box

Python+selenium Locating hidden elements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.