Skip to content
Advertisement

Finding button by XPath with Selenium

I have been trying to locate the following button using Selenium WebDriver:

<div class="_1gw6tte">
    <div aria-hidden="false">
        <div class="_159vfsnv" style="background-color: transparent;">
            <div class="_12to336">
                <div class="_1ibtygfe">
                    <button type="button" class="_pmpl8qu">
                    Afficher plus de résultats</button>
                </div>
            </div>
        </div>
    </div>
</div>

I used css selector, xpath, class and nothing seems to work (even by just copy pasting the one given by the inspector. The closest I’ve been is locating the div with the class _1ibtygfe) Here is everything I tried, I’m desperate I don’t understand why it does not locate it, it keeps throwing me a No Such Element exception.

wd.get(url)
print(wd.current_url) #check
#wd.find_element_by_xpath('//[@id="ExploreLayoutController"]/div[1]/div[2]/div[1]')#/div/div/div/button') #xpath given by inspector
#wd.find_element_by_class_name('_pmpl8qu')
#wd.find_element_by_xpath("//div[@class = '_1ibtygfe']/button[@class = '_pmpl8qu']")
#wd.find_element_by_xpath("//div[@class = '_1ibtygfe']/button")
#wd.find_elements_by_css_selector('button._pmpl8qu')

It’s the airbnb website, I’m trying to scrape their events and clicking on the see more button. Thanks for your help.

Advertisement

Answer

If you want to click this button you need to

  1. Close the “Accept cookies”
  2. Scroll to that button since in is out of the visible screen area.
  3. Click on it
    As following:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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

wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)

driver.get("https://fr.airbnb.be/s/Montreal--QC/experiences?tab_id=experience_tab&refinement_paths%5B%5D=%2Fexperiences&flexible_trip_dates%5B%5D=june&flexible_trip_dates%5B%5D=may&flexible_trip_lengths%5B%5D=weekend_trip&date_picker_type=calendar&source=structured_search_input_header&search_type=filter_change&rank_mode=default&place_id=ChIJDbdkHFQayUwR7-8fITgxTmU&checkin=2021-05-22")

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[data-testid="accept-btn"]"))).click()

button = driver.find_element_by_xpath("//button[contains(text(),'Afficher plus de résultats')]")
actions.move_to_element(button).build().perform()
time.sleep(0.5)
button.click()


User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement