RedVelvet
RedVelvet

Reputation: 1913

Click and view more pages with Selenium in Python

Problem I need to go into all the top user profile in this page using Selenium. The Top users profile are located in the right of the page.

What i've done

    self.driver.get(response.url)
    user_list = self.driver.find_elements_by_xpath('//table[contains(@class,"W-100 Bc-c")]/tbody/tr')
    for single_user in user_list:
        single_user.find_element_by_xpath('.//td/a').click()
        time.sleep(3)

But I get this error message:

WebDriverException: Message: unknown error: Element is not clickable at point (865, 685). Other element would receive the click:

<div id="MouseoverMask" class="End-0 Start-0 T-0 B-0"></div>

Info Python 2.7.10 Selenium 2.48 Pycharm

EDIT+

I try to make a print of the name and it works:

   print(str( single_user.find_element_by_xpath('.//td/a').text ) )

But the click() no.

Upvotes: 3

Views: 321

Answers (2)

Hayha
Hayha

Reputation: 2240

if you sure that the object you get is the right one, often the problem is:

  • The object is not visible
  • Page was not fully loaded when you try to click on the object.

So just take a look on the Wait method provided by Selenium and be sure your object is visible

In order to wait an element to be clickable :

from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID,'someid')))

In your case, you can try to find each element with the id you got and click on it :

self.driver.get(response.url)
user_list = self.driver.find_elements_by_xpath('//table[contains(@class,"W-100 Bc-c")]/tbody/tr')
for single_user in user_list:
    id = single_user.find_element_by_xpath('.//td/a').get_attribute("id")
    self.driver.find_elements_by_id(id).click()
    time.sleep(3) 

Upvotes: 1

Learner
Learner

Reputation: 5302

I don't see any error at my end but after first click web elements are changed so you will not get the next web element as captured earlier in xpath. By the way try below code-

from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get('https://answers.yahoo.com/dir/index/discover?sid=396545663')
user_list = driver.find_elements_by_xpath('//table[contains(@class,"W-100 Bc-c")]/tbody/tr')
lnks = [i.find_element_by_xpath('.//td/a').get_attribute('href') for i in user_list]
for single_user in lnks:
    driver.get(single_user)
    time.sleep(3)

Upvotes: 0

Related Questions