簡體   English   中英

如何點擊沒有名字的鏈接 selenium Python

[英]How to click on links with no names with selenium Python

單擊日期( https://www.eduqas.co.uk/qualifications/computer-science-as-a-level/#tab_pastpapers )后,我試圖單擊 web 頁面上的所有鏈接,但鏈接沒有唯一的 class 名稱,只有標簽名稱“a”,但多個其他元素具有相同的標簽名稱。 我怎樣才能點擊鏈接

這是當前代碼,它點擊日期但正如我所說我無法點擊鏈接:

from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

PATH = "C:\Program Files (x86)\chromedriver.exe"  # path of chrome driver
driver = webdriver.Chrome(PATH)  # accesses the chrome driver

driver.get("https://www.eduqas.co.uk/qualifications/computer-science-as-a-level/#tab_pastpapers")  # website
driver.maximize_window()

driver.implicitly_wait(3)
driver.execute_script("window.scrollTo(0, 540)")
sleep(3)  # Giving time to fully load the content
elements = driver.find_elements(By.CSS_SELECTOR, ".css-13punl2")
driver.find_element(By.ID, 'accept-cookies').click()  # Closes the cookies prompt


for x in elements:
    if x.text == 'GCSE':
        continue
    x.click()
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")  # this scrolls the page to the bottom
    sleep(1)  # This sleep is necessary to give time to finish scrolling



print(len(elements))

鏈接圖片

使用 XPath 的主要原因是當您沒有適合您希望定位的元素的 id 或 name 屬性時。 您可以使用 XPath 以絕對術語(不建議)或相對於具有 id 或 name 屬性的元素定位元素。 XPath 定位符也可用於通過 id 和 name 以外的屬性指定元素。

查找元素:

elem = driver.find_element(By.XPATH, "/html/body/form[1]")

獲取 XPath:

  • Go 轉巡檢 window
  • Select 所需的元素並右鍵單擊它
  • 在下拉菜單中單擊復制選項卡,然后單擊復制 XPath

希望這會有所幫助。快樂編碼:)

我想你正在嘗試這個:

# Needed libs
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time


driver = webdriver.Chrome()
driver.get('https://www.eduqas.co.uk/qualifications/computer-science-as-a-level/#tab_pastpapers')
driver.maximize_window()
actions = ActionChains(driver)

# We get how many dates (year) fields we have
dates_count = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//div[@id='pastpapers_content']//button[@class='css-13punl2']/..")))
driver.find_element(By.ID, 'accept-cookies').click()
# We do scroll to the year element and we click every year
for i in range(1, len(dates_count)+1):
    date = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, f"(//div[@id='pastpapers_content']//button[@class='css-13punl2']/..)[1]")))
    driver.execute_script("arguments[0].scrollIntoView();", date)
    time.sleep(0.3)
    date.click()
    time.sleep(0.3)
    # For every year we get all the links
    links = date = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, f"(//div[@class='css-1301qtx'])[{i}]//a")))
    # We do scroll to the link element and we click every link
    for link in links:
        driver.execute_script("arguments[0].scrollIntoView();", link)
        time.sleep(0.3)
        link.click()
        driver.switch_to.window(driver.window_handles[0])

這將打開頁面中的每個鏈接。

我希望代碼中的注釋有助於理解代碼的作用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM