我尝试只xpath可见元素,但是
# ============================================================
#import codecs
#import requests
#import html5lib
#import string
import lxml.html as lh
from lxml import etree
import urllib
import urllib2
import os
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from bs4 import BeautifulSoup
from pandas import *
import re
from datetime import datetime
from dateutil import parser
import time
import os
import inspect
import itertools
chromedriver = "chromedriver_win32.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
browser = webdriver.Chrome(chromedriver)
URL = 'http://odds.7m.hk/en/default.shtml?t=3&dt=2011-08-13'
browser.get(URL)
#expend the wrapped/collapsed event list which includes leagues
browser.find_element_by_xpath('//*[@id="hlistMatch"]').click()
#only omit the checkbox ENG Premier League id @value='92'
checkboxes = browser.find_elements_by_xpath('//input[@name="c_league" and not(@value="92") and @checked="checked"]')
for checkbox in checkboxes:
if checkbox.is_selected():
checkbox.click()
browser.find_element_by_xpath('//*[@id="league_input"]/span[1]/a').click()
browser.find_elements_by_xpath('//input[@id="bh473558"]/div')
Out[70]: []
为什么正常的find_element_by_xpath找不到[]?我想只得到可见的id元素。这里我附上我的截图通过下面的链接。谁能给我点盏灯?
我的问题——可见元素和不可见元素
需要xpath定位器为可见元素
你总是可以过滤掉不可见的元素:
var ele = Driver.FindElementsByXpath("xpath");
var visibleEle;
visibleEle.AddRange(ele.Where(t => t.Displayed));
我明白了,它的工作…但是代码太长了。
lnk = soup.findAll('a', attrs={'class':['team_ls','lot_icon0'],
'href':re.compile('http://data.7m.cn/matches_data/92/en/index.shtml|http://data.7m.cn/analyse/en/')})
EngPR = soup.findAll('a', href=re.compile('http://data.7m.cn/matches_data/92/en/index.shtml'))
matchID = []
df = lnk
for i in range(len(lnk)):
if EngPR[0]['href'] == lnk[i]['href']:
# re.findall(r'.*?([0-9]+)', dflist[0])
# Out[162]: ['7', '473558']
# [-1] to delete the 1st matched digit which is http://data.'7'm.cn
df = re.findall(r'.*?([0-9]+)', lnk[i+1]['href'])[-1]
matchID.append(df)
del lnk; del EngPR; del df; del i