简体   繁体   中英

How to click on links with no names with selenium Python

I'm trying to click on all the links on a web page after clicking on the dates ( https://www.eduqas.co.uk/qualifications/computer-science-as-a-level/#tab_pastpapers ) but the links don't have unique class names and only have a tag name "a" but multiple other elements have the same tag name. How can I click on the links

Here is the current code, it clicks on the dates but as I said I can't click on the links:

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))

Image of links

The main reason for using XPath is when you don't have a suitable id or name attribute for the element you wish to locate. You can use XPath to either locate the element in absolute terms (not advised) or relative to an element that does have an id or name attribute. XPath locators can also be used to specify elements via attributes other than id and name.

Find the elements:

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

Get XPath:

  • Go to the inspect window
  • Select the desired element and right-click on it
  • In the dropdown click on copy tab, then on copy XPath

Hope this helps.Happy Coding:)

I think you are trying this:

# 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])

This open every singlle link in the page.

I hope the comments into the code help to understand what the code does

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM