Frage

I tried to xpath only visible elements however

# ============================================================
#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]: []

Why normal find_element_by_xpath found nothing []? I'ld like to get only visible id element. Here I attach my screenshot via below link. Some body shade me a light?

My question --- visible and invisible elements

Need xpath locators for visible elements

War es hilfreich?

Lösung 2

I got it, its work... However the code is too long.

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

Andere Tipps

You could always filter out the elements which are not visible:

var ele = Driver.FindElementsByXpath("xpath");
var visibleEle;
visibleEle.AddRange(ele.Where(t => t.Displayed));
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top