gpt4 book ai didi

python-3.x - 无法单击标题(不可见元素)下的HREF

转载 作者:行者123 更新时间:2023-12-03 17:25:35 24 4
gpt4 key购买 nike

我想单击主标题下的所有“ 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";') 


为了解决这个问题。虽然这只是一个错误。关于如何解决此隐性元素问题有什么想法?

最佳答案

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请求每个页面的内容

关于python-3.x - 无法单击标题(不可见元素)下的HREF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46130333/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com