3

I'm very new to Selenium. I'm crawling data from this page. I need to scroll down the page and click on "Load More Arguments" to get more text. This is the location to click on.

<a class="debate-more-btn" href="javascript:void(0);" onclick="loadMoreArguments('15F7E61D-89B8-443A-A21C-13FD5EAA6087');">
      Load More Arguments
</a>

I have tried this code but it does not work. Should I need more code to locate to that (I think the 1 has already tell the location to click). Do you have any recommendation? Thank you in advance.

[1] btn_moreDebate = driver.find_elements_by_class_name("debate-more-btn")
[2] btn.click()
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
theteddyboy
  • 286
  • 1
  • 4
  • 13

2 Answers2

4

Find the link by link text, move to the element and click:

from selenium.webdriver.common.action_chains import ActionChains

link = driver.find_element_by_link_text('Load More Arguments')
ActionChains(browser).move_to_element(link).perform()
link.click()

If you get an exception while finding an element, you may need to use an Explicit Wait:

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

link = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Load More Arguments")))
ActionChains(browser).move_to_element(link).perform()
link.click()
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thank you. This is really helpful. It works. However, it only clicks on the text once. There are more "Load More Arguments" sections to click. I'm finding the solution to this problem. Also, when additional arguments show, the source code does not update. I'm wondering if there is a way that I can get the page source after the click. – theteddyboy Feb 06 '15 at 22:36
  • I studied the content here: http://stackoverflow.com/questions/26809954/splinter-or-selenium-can-we-get-current-html-page-after-clicking-a-button It is what I want. Thanks. – theteddyboy Feb 06 '15 at 23:29
2

If I understand your code correctly, I can see a few things wrong.
1. You're using find_elements_by_class_name. I'd recommend using find_element_by_class_name instead. elements returns a list, which isn't needed in a case where there is only one element.
2. You're using btn_moreDebate as the holder for the results of your find_elements, but then interacting with btn.

You should be able to perform the find and click in one action:

driver.find_element_by_class_name("debate-more-btn").click()
Richard
  • 8,961
  • 3
  • 38
  • 47
  • Yes it's my fault. I forgot to the btn_moreDebate. Also, thank you for the explanation of elements and element. I don't know about this before. Thanks. – theteddyboy Feb 06 '15 at 22:26