What are the hidden elements in the selenium operation
What are the hidden elements in the selenium operation, I believe many inexperienced people are at a loss about this, so this paper summarizes the causes and solutions of the problem, through this article I hope you can solve this problem.
Sometimes we encounter elements that are invisible, and selenium cannot manipulate them. For example, the following situation:
Python
The page mainly uses "display:none" to control the invisibility of the entire drop-down box. At this time, if you directly manipulate the drop-down box, you will be prompted:
From selenium import webdriverfrom selenium.webdriver.support.select import Selectimport os,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:elementnotvisible:Elementisnotcurrentlyvisibleandmaynotbemanipulated
We need to modify the value of display through javaScript.
…… Js = 'document.querySelectorAll ("select") [0] .style.display = "block";' driver.execute_script (js) sel = 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 select.
[0] specify the number of tags in this set of tags.
Style.display= "block"; display= "block" of the modified style to indicate visibility.
After executing this js code, you can operate the drop-down box normally.
Java
Here are the actions in java
Package com.jase.base;import java.io.File;import org.openqa.selenium.WebDriver;import org.openqa.selenium.By.ById;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.support.ui.Select;import org.openqa.selenium.JavascriptExecutor;public class SelectTest {public static void main (String [] args) {WebDriver driver = new ChromeDriver (); File file = new File ("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 = new Select (driver.findElement (ById.xpath ("/ / select")); sel.selectByValue ("opel");}}
After reading the above, have you mastered the methods of hidden elements in selenium operation? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!