无法在标题下单击HREF(不可见元素)

时间:2017-09-09 11:47:00

标签: python-3.x selenium xpath selenium-webdriver web-scraping

我想要点击主标题下的所有Href标签,然后导航到这些页面来抓取它们。为了工作的速度,我想要点击href而不必点击标题。我的问题是,有没有办法点击这些按钮,即使它不像右边的页面那样可见?它似乎对我不起作用。它似乎给了我:

Traceback (most recent call last):
  File "C:/Users/Bain3/PycharmProjects/untitled4/Centrebet2.py", line 58, in <module>
    EC.element_to_be_clickable((By.XPATH, '(//*[@id="accordionMenu1_ulSports"]/li/ul/li/ul/li/a)[%s]' % str(index + 1)))).click()
  File "C:\Users\Bain3\Anaconda3\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

我已经取代了

EC.element_to_be_clickable((By.XPATH, '(//*[@id="accordionMenu1_ulSports"]/li/ul/li/ul/li/a)[%s]' % str(index + 1)))).click()

driver.find_element_by_xpath('(//*[@id="accordionMenu1_ulSports"]/li/ul/li/ul/li/a)[%s]' % str(index + 1)).click()

然而,这似乎无法解决它,因为它只点击可见元素。

我的代码如下:

from random import shuffle
from selenium.webdriver.support.ui import WebDriverWait as wait

from selenium import webdriver as web
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from random import randint
from time import sleep
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import csv
import requests
import time
from selenium import webdriver

success = False
while not success:
    try:
        driver = webdriver.Chrome()
        driver.set_window_size(1024, 600)
        driver.maximize_window()
        driver.get('http://centrebet.com/')
        success = True
    except:
        driver.quit()

sleep(5)

sports = driver.find_element_by_id("accordionMenu1_ulSports")
if sports.get_attribute("style") == "display: none;":
    driver.find_element_by_xpath('//ul[@id="menu_acc"]/li[3]/a').click()

driver.find_element_by_xpath(".//*[@data-type ='sports_l1'][contains(text(), 'Soccer')]").click()

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

options = driver.find_elements_by_xpath('//*[@id="accordionMenu1_ulSports"]/li/ul/li/ul/li/a')

# Get list of inetegers [1, 2, ... n]
indexes = [index for index in range(len(options))]
# Shuffle them
shuffle(indexes)
for index in indexes:
    # Click on random option
    wait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '(//*[@id="accordionMenu1_ulSports"]/li/ul/li/ul/li/a)[%s]' % str(index + 1)))).click()

enter image description here

我也尝试过:

driver.execute_script('document.getElementByxpath("//*[@id="accordionMenu1_ulSports"]/li/ul/li/ul/li/a").style.visibility = "visible";') 

要解决这个问题。虽然这只是一个错误。关于如何解决这个隐形元素问题的任何想法?

2 个答案:

答案 0 :(得分:1)

您甚至可以尝试使用JavascriptExecutor

使用以下代码制作样式属性= display:block;

driver.execute_script("arguments[0].style.display = 'none'", driver.find_element_by_xpath("//*[@id='accordionMenu1_ulSports']/li/ul/li/ul"))

注意:确保使用正确的xpath。您的<ul>元素不会隐藏<a>,因此请仅使用该<ul>代码的xpath并尝试

答案 1 :(得分:1)

driver.execute_script('document.getElementByxpath("//*[@id="accordionMenu1_ulSports"]/li/ul/li/ul/li/a").style.visibility = "visible";')

给出错误,因为在Javascript中使用XPath不正确。 Correct way you can find here

要抓取所需数据,您可以使用以下代码:

import requests
import time
from selenium import webdriver

url = "http://centrebet.com/"
success = False
while not success:
try:
    driver = webdriver.Chrome()
    driver.set_window_size(1024, 600)
    driver.maximize_window()
    driver.get(url)
    success = True
except:
    driver.quit()

time.sleep(5)

sports = driver.find_element_by_id("accordionMenu1_ulSports")
links = [url + link.get_attribute("onclick").replace("menulink('", "").replace("')", "") for link in sports.find_elements_by_xpath('.//a[starts-with(@onclick, "menulink")]')]
for link in links:
    print(requests.get(link).text)

您可以使用HTTP-GET

请求每个页面的内容,而不是点击每个链接