Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the hidden elements in the selenium operation

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report