我试图通过使用selenium来获取一些上下文,但是我无法得到" display:none"部分内容。我尝试使用 [1]: https://i.stack.imgur.com/LdDL4.png
# -*- coding: utf-8 -*-
from selenium import webdriver
import time
from bs4 import BeautifulSoup
import re
from pyvirtualdisplay import Display
from lxml import etree
driver = webdriver.PhantomJS()
driver.get('http://flights.ctrip.com/')
driver.maximize_window()
time.sleep(1)
element_time = driver.find_element_by_id('DepartDate1TextBox')
element_time.clear()
element_time.send_keys(u'2017-10-22')
element_arr = driver.find_element_by_id('ArriveCity1TextBox')
element_arr.clear()
element_arr.send_keys(u'北京')
element_depart = driver.find_element_by_id('DepartCity1TextBox')
element_depart.clear()
element_depart.send_keys(u'南京')
driver.find_element_by_id('search_btn').click()
time.sleep(1)
print(driver.current_url)
driver.find_element_by_id('btnReSearch').click()
print(driver.current_url)
overlay=driver.find_element_by_id("mask_loading")
print(driver.exeucte_script("return arguments[0].getAttribute('style')",overlay))
driver.quit()
,但仍未按预期工作。
希望你能分享一些知识。
[这是html] [1]
{{1}}
答案 0 :(得分:0)
检索属性" display:none"您可以使用以下代码行:
String my_display = driver.findElement(By.id("mask_loading")).getAttribute("display");
System.out.println("Display attribute is set to : "+my_display);
答案 1 :(得分:0)
如果元素样式属性的值为display:none,则它是隐藏元素。基本上selenium不与隐藏元素交互。你必须使用selenium的javascript执行器与它进行交互。您可以获得下面给出的样式值。
WebElement overlay=driver.findElement(By.id("mask_loading"));
JavascriptExecutor je = (JavascriptExecutor )driver;
String style=je.executeScript("return arguments[0].getAttribute("style");", overlay);
System.out.println("style value of the element is "+style);
打印值“z-index:12; display:none;”
或者如果你想获得innerHTML,
String innerHTML=je.executeScript("return arguments[0].innerHTML;",overlay);
在Python中,
overlay=driver.find_element_by_id("mask_loading")
style =driver.exeucte_script("return arguments[0].getAttribute('style')",overlay)
或
innerHTML=driver.execute_script("return arguments[0].innerHTML;", overlay)