Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding element with explicit wait using selenium webdriver in python

I am trying to find the element with the link text, I am using following code

handle = driver.window_handles
#handle for windows
driver.switch_to.window(handle[1])
#switching to new window
link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Followers ")))

And I am getting following traceback

Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Followers ")))
File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until
raise TimeoutException(message)
TimeoutException: Message: ''

HTML of the element I am trying to select is

<a href="/Kevin-Rose/followers">Followers <span class="profile_count">43,799</span></a>

How can I solve this problem??

like image 941
Siddhesh Avatar asked Sep 15 '14 14:09

Siddhesh


People also ask

How do you wait and find an element in Selenium?

We have an explicit wait condition where we can pause or wait for an element before proceeding to the next step. The explicit wait waits for a specific amount of time before throwing an exception. To verify if an element is present, we can use the expected condition presenceOfElementLocated.

What is explicit wait in Selenium?

Explicit Wait in Selenium By using the Explicit Wait command, the WebDriver is directed to wait until a certain condition occurs before proceeding with executing the code. Setting Explicit Wait is important in cases where there are certain elements that naturally take more time to load.

Is WebDriver wait is an applicable for Find element and find elements?

WebDriverWait is applicable for findElement() and findElements().


1 Answers

If you use By.LINK_TEXT, there should be a link with exactly that text: Followers, but you have Followers 43,799.

In your case, you should use By.PARTIAL_LINK_TEXT instead:

wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, 'Followers')))

UPDATE Here's working example:

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

driver = webdriver.Chrome()  # CHANGEME
driver.get('http://www.quora.com/Kevin-Rose')
element = WebDriverWait(driver, 2).until(
    EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "Followers"))
)
element.click()
like image 157
falsetru Avatar answered Nov 15 '22 14:11

falsetru