0
votes

I am writing a bot that will follow the users on www.quora.com . following is the part of code I am using where I get timeout exception:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import urllib

driver = webdriver.Firefox()
driver.get('http://www.quora.com/')
time.sleep(10)

wait = WebDriverWait(driver, 10)

form = driver.find_element_by_class_name('regular_login')
time.sleep(10)
#add explicit wait

username = form.find_element_by_name('email')
time.sleep(10)
#add explicit wait

username.send_keys('[email protected]')
time.sleep(30)
#add explicit wait

password = form.find_element_by_name('password')
time.sleep(30)
#add explicit wait

password.send_keys('def')
time.sleep(30)
#add explicit wait

password.send_keys(Keys.RETURN)
time.sleep(30)

#search = driver.find_element_by_name('search_input')
search = wait.until(EC.presence_of_element_located((By.XPATH, "//form[@name='search_form']//input[@name='search_input']")))

search.clear()
search.send_keys('Kevin Rose')
search.send_keys(Keys.RETURN)

link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Kevin Rose")))
link.click()
#Wait till the element is loaded (Asynchronusly loaded webpage)

handle = driver.window_handles
driver.switch_to.window(handle[1])
#switch to new window 

element = WebDriverWait(driver, 2).until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "Followers")))
time.sleep(30)
element.click()
#goes to Kevin Rose followers page
time.sleep(30)

button = driver.find_elements_by_xpath("//a[contains(text(), 'Follow')]")
#Locate follow button on the page
no_of_followers = len(button)
#total number of unfollowed users
print no_of_followers


    while(no_of_followers > 0):
    # execute only if there are unfollowed users on page

        count = 1

        while(count < no_of_followers):

            time.sleep(30)
            link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Follow")))
            time.sleep(30)
            link.click()
            time.sleep(30)
            print count
            count = count + 1


        time.sleep(30)
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(30)
        button = driver.find_elements_by_xpath("//a[contains(text(), 'Follow')]")
        time.sleep(30)
        no_of_followers = len(button)

After executing the code i am getting "TimeoutException" error in the inner loop after successful execution once.

How can I solve this?

Traceback:

Traceback (most recent call last): File "C:\Python27\quorabot7", line 72, in link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Follow"))) File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until raise TimeoutException(message) TimeoutException: Message: ''

2
Please include the full Traceback. - dano
Also, please post the complete code, so that we can reproduce it and debug step by step. Thanks. - alecxe
Posted complete code and traceback. - Siddhesh

2 Answers

3
votes

You're getting a TimeoutException because Selenium can't find that element within the time that you've set as your wait. This means that your locator strategy is incorrect.

I have not tested your other locators, but if it is truly the inner loop that is failing... my solution is below.

After looking through the DOM on Kevin Hart's page, I can see that the button you're interested in is:

<a class="follow_button with_count" href="#" action_click="UserFollow" id="__w2_Mab4s9V_follow_user">Follow<span class="count">43.8k</span></a>

You should try this:

link = wait.until(EC.presence_of_element_located(\
       (By.className, "follow_button with_count")))

or this:

link = wait.until(EC.presence_of_element_located(\
       (By.XPATH, '//a[@action_click="UserFollow"]')))
0
votes

The correct is By.CLASS_NAME, not By.ClassName