我尝试单击此处页面上的“>”,但 chrome XPATH 或 CSS 选择器找不到它 https://theanalyst.com/na/2023/08/opta-football-predictions/
chrome full xpath 应该只适用于 selenium 吗?
import sys
import os
import pandas as pd
from selenium import webdriver
from pyvirtualdisplay import Display
import feyn_common
from bs4 import BeautifulSoup
from selenium import webdriver
import chromedriver_binary
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
def parse_website():
# Start a virtual display
display = Display(visible=0, size=(800, 600))
display.start()
try:
# Set Chrome options with the binary location
chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location = "/usr/bin/google-chrome"
# Initialize Chrome driver
driver = webdriver.Chrome()
# Open the desired URL
url = "https://theanalyst.com/na/2023/08/opta-football-predictions/"
driver.get(url)
# Wait for the page to load completely (adjust the time as needed)
# Parse the page source using BeautifulSoup
predictions = WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located(
(By.CSS_SELECTOR, "iframe[src*=predictions]")
)
)
element = driver.find_element_by_css_selector("#pos_3.div.fixtures-header.button:nth-child(3)")
element.click()
认为有两个问题 - 第一个问题,您必须切换到 iframe 才能与其内容交互:
...
# Wait for the page to load completely (adjust the time as needed)
# Parse the page source using BeautifulSoup
driver.switch_to.frame(WebDriverWait(driver, 10).until(
EC.presence_of_element_located(
(By.CSS_SELECTOR, "#iFrameResizer1")
)
))
其次,检查你的选择器,没有带有值的id
pos_3
element = driver.find_element(By.CSS_SELECTOR,"#pos_2 button")
element.click()