1

I tried to use webdriver to login to two websites, A and B. But when I use the same way "find_element_by_css_selector", B was not working while A was working. I turned the javascript off, and find B's login section disappeared.

A's html:

<button type="submit" class="width-35 pull-right btn btn-sm btn-primary">
  <i class="ace-icon fa fa-key"></i>
  <span class="bigger-110">Login</span>
</button>

Code of A is working well:

submit=driver.find_element_by_css_selector(".width-35.pull-right.btn.btn-sm.btn-primary").click()

B's html:

<a class="login-btn" href="javascript:;" data-bind="click: loginSection.loginClick">
  <span class="btn-text">Login</span>
</a>

Code of B is not working:

submit=driver.find_element_by_css_selector("a.login-btn > span.btn-text").click()

Error says:

ElementNotVisibleException: Message: element not visible

I posted another question before, Python: find_element_by_css_selector, and someone suggested me to use "find_elements_by_link_text" and it works, but it only works with that one. I would still like to know how to solve this problem. Thanks!

Updated:

Link=WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='login-btn'][@class='login-btn']"))).click()

Error:

TimeoutException: Message: 
3
  • Hi @Lara19, have you considered using XPath? Commented Feb 5, 2018 at 1:52
  • @Ali I can try! :) Commented Feb 5, 2018 at 1:57
  • See if my answer solves your issue, if it doesn't then I believe you will need to add delays right before the line that clicks on the a tag in html B. Commented Feb 5, 2018 at 2:04

2 Answers 2

3

While you've already got an accepted answer, I want you to know the root cause of your issue

There are 2 links with the same class names: first one is hidden. You can check it with len(driver.find_elements_by_class_name("login-btn")).

That's why your code

Link=WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='login-btn'][@class='login-btn']"))).click()

gives you TimeOutException - hidden element cannot be clickable

You can fix it by specifying index of button:

Link = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(//a[@class='login-btn'])[2]")))
Link.click()

or using search by link text:

Link = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "確定登入")))
Link.click()

Note that search by link text will skip hidden link and handle only the visible one

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

4 Comments

Thank you! Yeah it works :D Just a question, does [2] represent its second root [@class='login-btn']?
In XPath indexation starts from 1, but not from 0 as in Python
which means that you can find two class='login-btn' and what I need it the second one?
Yeap. (//a[@class='login-btn'])[2] means get list of links with class name "login-btn" and return the second one
0

I used the time.sleep() delay approach and it actually worked on my end.

from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get('https://www.easyauction.com.tw/index.html')

########
# Username and password go here
########

time.sleep(10)
driver.find_element_by_css_selector('#IndexLogin > div > form > a.login-btn > span').click()
driver.quit()

9 Comments

Is it possible to provide me the URL you accessing?
But I cannot provide you the username and password, is that okay?
No problem as long as I can view the same html content you're viewing.
It's in Chinese. The red button is the login button
|

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.