Search code examples
pythonselenium-webdriverbeautifulsouponclickclick

Selenium unable to find element that soup can so unable to click using python selenium and Beautiful soup


I am trying to click on a onclick item but Selenium doesn't find it. Element is as follows.

<a tabindex="0" onkeypress="loginKeyboardEvent(event)" role="button" aria-pressed="false" class="waves-effect waves-light"> Login </a>

I have tried variations on the following. BeautifulSoup finds the element but Selenium doesn't. I have found in BS that the element is in the 4th div of 'wave-effect...' Selenium returns an empty list so cannot find even the first instance of the class='wave...'

driver.get("https://www.woolworthsrewards.com.au/")
time.sleep(5)
page_source=driver.page_source
soup = BeautifulSoup(page_source, 'lxml')
products_selector = soup.find_all('a', class_='waves-effect waves-light')
login_link=(products_selector[3].text)
print(login_link)

p_sel=driver.find_elements(By.CLASS_NAME,'waves-effect waves-light')
print(p_sel)

Using selenium to get page-source does not return the element but BS does. Though using the xpath in selenium still does not find the element

How can I send a click event with selenium when it can't find the element and BS does not have a click event. Once clicked it will open a side panel to enter login details, though if I can't open the side panel, I can't get to enter the login details. the xpath to the anchor is

xpath - /html/body/div[2]/div[2]/div/div[2]/wr-header-element/header/nav/div/div/div[1]/ul/li[4]/a

but again, selenium does nto find it


Solution

  • Here is one way to access those links. Assuming you have a working Selenium setup:

    [...]
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    [...]
    wait = WebDriverWait(driver, 25)
    
    url = 'https://www.woolworthsrewards.com.au/'
    driver.get(url)
    
    for e in wait.until(EC.presence_of_all_elements_located((By.XPATH, '//a[@class="waves-effect waves-light"]'))):
        print(e.text)
    

    Result in terminal:

    PARTNERS
    HELP & FAQS
    HOW IT WORKS
    LOGIN
    JOIN
    

    Selenium documentation can be found here.