holder
holder

Reputation: 113

selenium using python: how to correctly click() an element?

while learning how to use selenium, Im trying to click an element but nothing happens and Im unable to reach the next page. this is the relevant page: http://buyme.co.il and Im trying to click: הרשמה

I managed to print the desired element (הרשמה) so I guess Im reaching the correct place in the page. but 'click()' doesnt work. the second span <span>הרשמה</span> is what i want to click:

<li data-ember-action="636">
        <a>
            <span class="seperator-link">כניסה</span>
            <span>הרשמה</span>
        </a>
 </li>
for elem in driver.find_elements_by_xpath('//* [@id="ember591"]/div/ul[1]/li[3]/a/span[2]'):
    print (elem.text)
    elem.click()

also tried this:

driver.find_element_by_xpath('//*[@id="ember591"]/div/ul[1]/li[3]/a').click()

I expected to get to the "lightbox" which contain the registration fields. Any thoughts on the best way to accomplish this?

Upvotes: 2

Views: 1408

Answers (2)

bharatk
bharatk

Reputation: 4315

Explicit Waits - An explicit wait is a code you define to wait for a certain condition to occur before proceeding further in the code.

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

browser = webdriver.Chrome()
browser.get("https://buyme.co.il/")

WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID, 'ember591')))

elm = browser.find_elements_by_xpath('//div[@id="ember591"]/div/ul[1]/li[3]/a')

elm[0].click()

enter image description here

Update:

WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, 'login')))

email = browser.find_elements_by_xpath("//form[@id='ember1005']/div[1]/label/input")
email[0].send_keys("[email protected]")

password = browser.find_elements_by_xpath("//form[@id='ember1005']/div[2]/label/input")
password[0].send_keys("test1234567")

login = browser.find_elements_by_xpath('//form[@id="ember1005"]/button')
login[0].click()

enter image description here

Upvotes: 2

undetected Selenium
undetected Selenium

Reputation: 193078

The desired element is an Ember.js enabled element so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use the following Locator Strategy:

  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='הרשמה']"))).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
    

Upvotes: 0

Related Questions