0

[updates below]

i'm trying to click a result from this list: http://www.imdb.com/find?ref_=nv_sr_fn&q=walking+dead&s=all

there are parameters i'm trying to match before selecting, so i find elements first, then try to click the child element of the one that matches. in this case, it would be the first link, for the 2010 series

right now i have this, which does not result in an error, but the link is not actually getting clicked. can someone please help me figure this out?

i get a list of the result elements

element = browser.find_elements_by_xpath('//td[contains(@class, "result_text")]')

after filtering, the first result is what i want. element[0] is 'The Walking Dead (2010) (TV Series)'. the link part of that is found by searching for href

element = element[0].find_element_by_xpath('//a[contains(@href, "title")]')
actions = ActionChains(browser)
actions.click(element).perform()

relevent element inspection

update1: element.click() does not work

Traceback (most recent call last):
  File "U:\backup\references\python practice\episodes.py", line 41, in <module>
    element.click()
  File "C:\Python27\lib\site-packages\selenium-3.0.1-py2.7.egg\selenium\webdriver\remote\webelement.py", line 77, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Python27\lib\site-packages\selenium-3.0.1-py2.7.egg\selenium\webdriver\remote\webelement.py", line 494, in _execute
    return self._parent.execute(command, params)
  File "C:\Python27\lib\site-packages\selenium-3.0.1-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium-3.0.1-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
  (Session info: chrome=54.0.2840.99)
  (Driver info: chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 6.1.7601 SP1 x86_64)

update2: tried to wait for visibility of element

element = browser.find_elements_by_xpath('//td[contains(@class, "result_text")]')
elementchild = element[x].find_element_by_xpath('//a[contains(@href, "title")]')
elementvisibility = WebDriverWait(browser, 60).until(
    EC.visibility_of_element_located((By.XPATH, '//a[contains(@href, "title")]'))
)
elementchild.click()

but am getting timeout exceptions

Traceback (most recent call last):
  File "U:\backup\references\python practice\episodes.py", line 44, in <module>
    EC.visibility_of_element_located((By.XPATH, '//a[contains(@href, "title")]'))
  File "C:\Python27\lib\site-packages\selenium-3.0.1-py2.7.egg\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

what's weird is that the element can be found with the 'elementchild...' line, but it isn't visible even with a 60s timer. can javascript load that much slower?

  • The error from `element.click()` is another issue - judging by the error, it looks like the link is loaded via JavaScript and simply doesn't exist when you call the `click` method. As a temporary hack to investigate, try adding a `time.sleep(3)` to wait 3 seconds just before the `click()`. See if that works and let me know. – That1Guy Nov 29 '16 at 16:56
  • i still get the element not visible error. is there a way to verify that that link is loaded via javascript before attempting to run the script? also, is there a way to click without using java? – baconpancakes Nov 29 '16 at 16:59
  • Yes there is a way to explicitly wait for the element to exist. How are you running your script? Is it running headless or are you able to watch the browser while the script is running? – That1Guy Nov 29 '16 at 17:20
  • so javascript loaded links are just slower? i'm running through eclipse, and a new instance of the browser appears with each execution – baconpancakes Nov 29 '16 at 18:45
  • Its not that they're "loaded slower" its that, usually, JavaScript-loaded content appears after the page itself has loaded, so there exists a race condition where Selenium is looking for content (because the page has loaded) but can't find it because it hasn't been loaded by JS yet. – That1Guy Nov 29 '16 at 18:57
  • Since you're able to watch the browser when your script is running, can you verify the link exists prior to the `element.click` call? – That1Guy Nov 29 '16 at 19:00
  • yes, the link is visible, and when i bring the mouse over, it is clickable, but the script is not able to see it – baconpancakes Nov 29 '16 at 19:36

1 Answers1

1

Instead of using ActionChains simply call the click method of element:

element = element[0].find_element_by_xpath('//a[contains(@href, "title")]')
element.click()

If the element is loaded after the page and cannot be found immediately, you can explicitly wait for the element to be present before calling the click method.

Here is an example from this answer:

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()
Community
  • 1
  • 1
That1Guy
  • 7,075
  • 4
  • 47
  • 59
  • 'presence_of_element_located' still gives me 'element not visible', so i think that i need 'visibility_of_element_located', but is there a way to use my previous 'element[0]' as i did in 'element = element[0].find_element_by_xpath('//a[contains(@href, "title")]')'? i have 'EC.visibility_of_element_located((By.XPATH, '//a[contains(@href, "title")]'))', but i don't know how to combine with 'element[0]' – baconpancakes Nov 29 '16 at 19:32