9

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??

falsetru
  • 357,413
  • 63
  • 732
  • 636
Siddhesh
  • 472
  • 1
  • 9
  • 24

2 Answers2

11

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()
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • It gave the same error. I tried finding different elements using ids and classes its giving same error. I also downloaded the html source of the page to make sure I am on the same page. This is the link to the page "http://www.quora.com/Kevin-Rose" I am trying to click on followers link. – Siddhesh Sep 15 '14 at 15:57
  • @Siddhesh, I added a working example. The problem was the trailing space. Please check it out. – falsetru Sep 15 '14 at 16:21
  • 1
    How does one know what arguments exist to go after ``By.``? I have a hard time finding documentation on that.. – altabq Feb 01 '16 at 11:09
  • Thanks a lot! That was quick :) – altabq Feb 01 '16 at 13:02
  • For anyone wondering how to find the By. options. If you have PyCharm or something similar, import the package at the top of your script and literally write By. and you will see all the options appear in the dropdown. I know it's super simple and obvious, but I went Googling before I bothered to check. – Danbardo Nov 29 '20 at 12:15
0

Cheers

from selenium import webdrivercode
from selenium.webdriver.common.by import By
import selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("http://somedomain/url_that_delays_loading")

#Follow below syntax
element = WebDriverWait(driver, 10)
element.until(EC.presence_of_element_located((By.ID, '--webElement--')))