0

I'm trying to click a "load more" button with the following code

browser.find_element_by_xpath('//*[@id="mainContent"]/div[1]/div/div[5]/div/div[1]').click()
browser.find_element_by_css_selector('#mainContent > div.left-panel > div > div.result-list > div > div.content').click()
browser.find_element_by_link_text('Load More').click()

I receive the following error:

Traceback (most recent call last):
  File "C:\Users\David\eclipse-workspace\Web_Scrap\setup.py", line 38, in <module>
    browser.find_element_by_css_selector('#mainContent > div.left-panel > div > div.result-list > div > div.content').click()
  File "C:\Users\David\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\David\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\David\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
    self.error_handler.check_response(response)
  File "C:\Users\David\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Element is not clickable at point (239, 698)
  (Session info: chrome=68.0.3440.106)
  (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)

I've tried each of those 3 individually but can't seem to get selenium to click the button

When pressing inspect that is the following element I receive

The element code is as follows:

<div class="content" onclick="javascript:mtvn.btg.Controller.sendLinkEvent({ linkName:'PROFMIDPANE:LoadMore', linkType:'o' } );">Load More</div>

if anyone has any recommendations on how I can achieve this I would greatly appreciate it!

UPDATE: I tried the two solutions recommended to me but unfortunately didn't work out, I will post it here if anyone is interested.

iamsankalp89 solution:

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='content' and text()='Load More']")))
element.click()

Error message:

Traceback (most recent call last):
  File "C:\Users\David\eclipse-workspace\Web_Scrap\setup.py", line 39, in <module>
    element.click()
  File "C:\Users\David\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\David\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\David\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
    self.error_handler.check_response(response)
  File "C:\Users\David\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Element is not clickable at point (239, 698)
  (Session info: chrome=68.0.3440.106)
  (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)

Julian Moreno solution:

ActionChains(driver).move_to_element("//div[@class='content' and text()='Load More']").click("//div[@class='content' and text()='Load More']").perform()

Error Message:

Traceback (most recent call last):
  File "C:\Users\David\eclipse-workspace\Web_Scrap\setup.py", line 42, in <module>
    ActionChains(browser).move_to_element("//div[@class='content' and text()='Load More").click("//div[@class='content' and text()='Load More").perform()
  File "C:\Users\David\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\action_chains.py", line 83, in perform
    action()
  File "C:\Users\David\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\action_chains.py", line 293, in <lambda>
    Command.MOVE_TO, {'element': to_element.id}))
AttributeError: 'str' object has no attribute 'id'
david yeritsyan
  • 422
  • 6
  • 26

2 Answers2

1

Try this xpath

//div[@class='content' and text()='Load More']

The code is like this:

browser.find_element_by_xpath('//div[@class='content' and text()='Load More']').click()

Also use WebDriverWait

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='content' and text()='Load More']")))
element.click()
iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
0

Try using ActionChains:

class selenium.webdriver.common.action_chains.ActionChains(driver)

ActionChains(driver).move_to_element(your_element).click(your_element).perform()

The move to element will move the mouse (cursor) to the middle of the element (your_element) then the perform function will perform the actions chained.

EDIT

Try this:

load_more = browser.find_element_by_css_selector("#mainContent > div.left-panel > div > div.result-list > div > div.content")
WebDriverWait(browser, timeout).until(EC.visibility_of(load_more))
browser.execute_script("return arguments[0].scrollIntoView(true);", load_more)
ActionChains(browser).move_to_element(load_more).click().perform()

The ActionChains(browser).move_to_element() accepts WebElement object or String which is the id of the element. Since the load more does not have an ID then the move_to_element() could not find the WebElement.

After analysing the web page, I noticed that move_to_element(load_more) moves to the load_more button (only until it is within the screen) but it also triggers scrolling down and that displays the footer of the web page. This footer covers the load_more button. Therefore, you need the browser.execute_script("return arguments[0].scrollIntoView(true);", load_more) which will basically keep scrolling until load_more is in view (regardless of the footer showing up).

  • ActionChains(browser).move_to_element("//div[@class='content' and text()='Load More").click("//div[@class='content' and text()='Load More").perform() (Claims that it isn't defined, even though when I do ctrl + click, it shows the class) – david yeritsyan Sep 04 '18 at 09:32
  • I've edited my answer, let me know of your progress. Happy coding :) – Julian Moreno Sep 05 '18 at 11:29
  • Thank you so much!, at first it wasn't working and a classmate asked told me to try closing a pop up which was causing it not to work, so I followed the same logic with the action class that you gave me and it worked THANK YOU SOOO SOO MUCH – david yeritsyan Sep 05 '18 at 17:23