Reputation: 699
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:
Upvotes: 1
Views: 1823
Reputation: 1357
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()
Upvotes: 0
Reputation: 52665
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
Upvotes: 3