1

I am trying to automate logging into a website (http://www.phptravels.net/) using Selenium - Python on Chrome. This is an open website used for automation tutorials.

I am trying to click an element to open a drop-down (My Account button at the top navbar) which will then give me the option to login and redirect me to the login page.

The HTML is nested with many div and ul/li tags.

I have tried various lines of code but haven't been able to make much progress.

 driver.find_element_by_id('li_myaccount').click()
 driver.find_element_by_link_text(' Login').click()
 driver.find_element_by_xpath("//*[@id='li_myaccount']/ul/li[1]/a").click()

These are some of the examples that I tried out. All of them failed with the error "element not visible".

How do I find those elements? Even the xpath function is throwing this error. I have not added any time wait in my code.

Any ideas how to proceed further?

0

2 Answers 2

1

Hope this code will help:

from selenium import webdriver
from selenium.webdriver.common.by import By

url="http://www.phptravels.net/"
d=webdriver.Chrome()
d.maximize_window()

d.get(url)


d.find_element(By.LINK_TEXT,'MY ACCOUNT').click()


d.find_element(By.LINK_TEXT,'Login').click()

d.find_element(By.NAME,"username").send_keys("Test")
d.find_element(By.NAME,"password").send_keys("Test")
d.find_element(By.XPATH,"//button[text()='Login']").click()

Use the best available locator on your html page, so that you need not to create xpath of css for simple operations

Sign up to request clarification or add additional context in comments.

2 Comments

I am just curious - why cant we use the same d.find_element(By.LINK_TEXT, 'Login').click() on the second page? I get element not found error when we try that. Is there any logic to when to use LINK_TEXT or xpath ?
because on second page Login is a button not a link(Hope you are talking about login page)
1

You may be having issues with the page not being loaded when you try and find the element of interest. You should use the WebDriverWait class to wait until a given element is present in the page.

Adapted from the docs:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Set up your driver here....

try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, 'li_myaccount'))
    )
    element.click()
except:
    #Handle any exceptions here
finally:
    driver.quit()

1 Comment

Adding the WebDriverWait did not help. I changed the wait time ranging from 10 - 60 but still had the same error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.