Selenium webdriver--manipulating hidden elements

Source: Internet
Author: User

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') Select (SEL). Select_by_value ('Opel') Time.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"; '  = 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.

Java code

ImportJava.io.File;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.By.ById;ImportOrg.openqa.selenium.chrome.ChromeDriver;ImportOrg.openqa.selenium.support.ui.Select;ImportOrg.openqa.selenium.JavascriptExecutor; Public classSelecttest { Public Static voidMain (string[] args) {Webdriver driver=NewChromedriver (); File File=NewFile ("c:/users/fnngj/desktop/test.html"); String FilePath=File.getabsolutepath ();                 Driver.get (FilePath); String JS= "Document.queryselectorall (' select ') [0].style.display= ' block ';;                ((Javascriptexecutor) driver). Executescript (JS); Select sel=NewSelect (Driver.findelement (Byid.xpath ("//select"))); Sel.selectbyvalue ("Opel"); }}

Selenium webdriver--manipulating 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.