Berimbolinho
Berimbolinho

Reputation: 566

Can't find element on webpage with Selenium

I'm trying to build a bot that will book a class for me when I run it.

But I'm confused about how to proceed.

I'm using Python and Selenium with Chromedriver to load in the page.

The next step is to click the login button, add my data, log in and then navigate the page to find the correct class.

I'm very new to this so it might be very obvious but I can't figure it out.

I'm running this code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

d = webdriver.Chrome()

d.get("http://www.360functionalfitness.se/boka-pass/")

elem = d.find_element_by_name("showLogin")

The following error:

python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="showLogin"]"}
  (Session info: chrome=87.0.4280.141)

I've been reading a bunch and still can't figure out how to identify the dang button.

This is the block I think I'm supposed to look at to find the relevant info:

<li ng-if="!member" class="ng-scope">
            <a href="" ng-click="showLogin()" toggle-off-canvas=""><i class="fa fa-sign-in"><span class="icon-bg bg-darkgreen"></span></i><span class="ng-binding">Logga in</span></a>
        </li>
   

Any pointers would be extremely helpful.

Upvotes: 1

Views: 1361

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193308

The element with the text as Logga in is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      driver.get("http://www.360functionalfitness.se/boka-pass/")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://crossfitmalmo.gymsystem.se/member#/schedule']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[ng-click^='showLogin'] span.ng-binding"))).click()
      
    • Using XPATH:

      driver.get("http://www.360functionalfitness.se/boka-pass/")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'https://crossfitmalmo.gymsystem.se/member#/schedule')]")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@ng-click, 'showLogin')]//span[@class='ng-binding']"))).click()
      
  • Note : You have to add the following imports :

     from selenium.webdriver.support.ui import WebDriverWait
     from selenium.webdriver.common.by import By
     from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

Logga


Reference

You can find a couple of relevant discussions in:

Upvotes: 1

Related Questions