nick
nick

Reputation: 1178

Selenium can't find xpath or css selector

I'm trying to scrape this site that has pagination. The problem I'm facing is having selenium locate the next button.

What I've tried:

next_button = driver.find_element_by_css_selector(
    'ul[class="css-12ke8jn e65zztl0"] button[aria-label="Next"]').click()

and

page_amount = driver.find_element_by_css_selector(
    '/html/body/div[1]/div[2]/div/div/div/div[2]/div[1]/main/div[3]/div/div[2]/div[2]/nav/ul/button').click()

None of these work and I'm kinda stuck. The reason I'm using aria-label for the first one is because when the next button is selected the previous button changes to the same class as the next button. Note: The button is inside a ul.

Upvotes: 1

Views: 2894

Answers (3)

Karan Verma
Karan Verma

Reputation: 37

next_button = driver.find_element_by_xpath('//button[@class="css-1lkjxdl eanm77i0"]').click()

You was using xpath variable and finding it by css. for css selector you have to use the class (.css-1lkjxdl) and use the above code it will work and accept the answer. Thanks!!

Upvotes: 1

Alin Stelian
Alin Stelian

Reputation: 897

It might not work finding the element because it's not visible in UI - it is loaded but not visible, the easiest way is to move to that element and click on it.

next_button = driver.find_element_by_css_selector('[aria-label=\'Next\']')
actions = ActionChains(driver)
actions.move_to_element(next_button).perform()
next_button.click()

Upvotes: 2

Laurent LAPORTE
Laurent LAPORTE

Reputation: 22992

aria-label is an attribute, not an element.

Your xpath should be fixed as follow:

button[@aria-label="Next"]

To find this button anywhere in the page, you can try:

//button[@aria-label="Next"]

Then, you can try:

button = driver.find_element_by_xpath('//button[@aria-label="Next"]')

Upvotes: 0

Related Questions