网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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

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

成都创新互联公司专注于盖州网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供盖州营销型网站建设,盖州网站制作、盖州网页设计、盖州网站官网定制、微信小程序开发服务,打造盖州网络公司原创品牌,更为您提供盖州网站排名全网营销落地服务。

Python

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

from selenium import webdriver
from selenium.webdriver.support.select import Select
import os,time

driver = 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

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

……

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")选择所有的select。

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

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

执行完这句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 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");
 
 }
}

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


名称栏目:使用selenium怎么操作隐藏的元素
本文路径:http://bjjierui.cn/article/pocicg.html

其他资讯