使用硒怎么操作隐藏的元素

  介绍

这篇文章将为大家详细讲解有关使用硒怎么操作隐藏的元素,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

<强> Python

页面主要通过“显示:没有”来控制整个下拉框不可见。这个时候如果直接操作这个下拉框,就会提示:

得到selenium  import  webdriver   得到selenium.webdriver.support.select  import 选择   import 操作系统时间      时间=driver  webdriver.Chrome ()   时间=file_path  & # 39;文件:///& # 39;,+,os.path.abspath (& # 39; test.html& # 39;)   driver.get (file_path)      时间=sel  driver.find_element_by_tag_name(& # 39;选择# 39;)   选择(sel) .select_by_value(& # 39;欧宝# 39;)   time . sleep (2)      driver.quit ()

<强>例外。ElementNotVisibleException:消息:elementnotvisible: Elementisnotcurrentlyvisibleandmaynotbemanipulated

我们需要通过javaScript修改显示的值。

……      时间=js  & # 39; document.querySelectorAll(“选择”)[0].style.display=癰lock"; & # 39;   driver.execute_script (js)      时间=sel  driver.find_element_by_tag_name(& # 39;选择# 39;)   选择(sel) .select_by_value(& # 39;欧宝# 39;)      以前……

document.querySelectorAll(“选择”)[0].style.display=癰lock";

document.querySelectorAll(“选择”)选择所有的选择。

[0]指定这一组标签里的第几个。

style.display=癰lock";修改样式的显示=癰lock",表示可见。

执行完这句js代码后,就可以正常操作下拉框了。

<强> Java

以下为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 文件(“C:/用户/fnngj/桌面/test.html");   String 才能;filePath =, file.getAbsolutePath ();   driver.get才能(filePath);   ,,   ,,String  js =,“document.querySelectorAll(& # 39;选择# 39;)[0].style.display=& # 39;块# 39;;“   ((JavascriptExecutor)驱动程序)才能.executeScript (js);   ,,   Select 才能;sel =, new 选择(driver.findElement (ById.xpath(“//选择”)));   sel.selectByValue才能(“opel");   ,   ,}   }

关于使用硒怎么操作隐藏的元素就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看的到。

使用硒怎么操作隐藏的元素