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