selenium操作隱藏的元素(python+Java)
有時(shí)候我們會(huì)碰到一些元素不可見(jiàn),這個(gè)時(shí)候selenium就無(wú)法對(duì)這些元素進(jìn)行操作了。例如,下面的情況:

Python
頁(yè)面主要通過(guò)“display:none”來(lái)控制整個(gè)下拉框不可見(jiàn)。這個(gè)時(shí)候如果直接操作這個(gè)下拉框,就會(huì)提示:
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
我們需要通過(guò)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]指定這一組標(biāo)簽里的第幾個(gè)。
style.display="block";修改樣式的display="block",表示可見(jiàn)。
執(zhí)行完這句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("http://select")));
sel.selectByValue("opel");
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JFreeChart實(shí)現(xiàn)實(shí)時(shí)曲線圖
這篇文章主要為大家詳細(xì)介紹了JFreeChart實(shí)現(xiàn)實(shí)時(shí)曲線圖的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
springboot打war包部署到外置tomcat容器的方法
這篇文章主要介紹了springboot]打war包部署到外置tomcat容器,在這需要注意的是在boot-launch.war在tomcat?webapps目錄里面解壓到boot-launch文件夾,感興趣的朋友跟隨小編一起看看吧2022-04-04
詳解Spring Boot + Mybatis 實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源
這篇文章主要介紹了Spring Boot + Mybatis 實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
深入理解MyBatis中的一級(jí)緩存與二級(jí)緩存
這篇文章主要給大家深入的介紹了關(guān)于MyBatis中一級(jí)緩存與二級(jí)緩存的相關(guān)資料,文中詳細(xì)介紹MyBatis中一級(jí)緩存與二級(jí)緩存的工作原理及使用,對(duì)大家具有一定的參考性學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-06-06
Java 實(shí)現(xiàn)FTP服務(wù)實(shí)例詳解
這篇文章主要介紹了Java 實(shí)現(xiàn)FTP服務(wù)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
使用MyBatis進(jìn)行簡(jiǎn)單的更新與查詢方式
這篇文章主要介紹了使用MyBatis進(jìn)行簡(jiǎn)單的更新與查詢方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Java生成Echarts表圖的2種實(shí)現(xiàn)方案
這篇文章主要給大家介紹了關(guān)于Java生成Echarts表圖的2種實(shí)現(xiàn)方案,ECharts是一款功能非常強(qiáng)大的JavaScript圖表庫(kù),文中通過(guò)代碼實(shí)例介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09

