1

I am scraping a page heavy with JS, where I select a js button

driver.find_element_by_css_selector('.u-textColorDarker').click()

which displays a from. Inside that form, some info is displayed, but to get all the content there is a button ("show more") that needs to be clicked n times to display all the content. When there is nothing else to show, the button disappears.

In this particular page, the button needs to be clicked 3 times. So a quick solution for my problem in:

driver.find_element_by_css_selector('.js-showMoreRecommends').click()
time.sleep(2)
driver.find_element_by_css_selector('.js-showMoreRecommends').click()
time.sleep(2)
driver.find_element_by_css_selector('.js-showMoreRecommends').click()
time.sleep(2)

Is there a way to put this in a loop, so the action is done inside a loop until the selector '.js-showMoreRecommends' is no longer present?

1
  • 1
    Sure, use while True: and break out of it when your condition is met Commented Jun 18, 2019 at 12:30

3 Answers 3

2

Yes, you can using expected conditions.

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

driver = webdriver.Firefox()
driver.get("http://somedomain/url")

while not EC.invisibility_of_element_located((By.CSS_SELECTOR, ".js-showMoreRecommends")):
    driver.find_element_by_css_selector('.js-showMoreRecommends').click()

I am writing this from memory, but it should point you in the general direction. The meat of this is the expected conditions method EC.invisibility_of_element_located. It will give you a boolean value to determine if it is gone yet.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah we were writing posts about the same time.
Thank you Raven, this solution executed, but the buttons did not get clicked. Jose Martinez solution worked well. Not really sure why yours did not work. Thanks again, anyway
Probably just needed a different expected conditions selector. Glad you found a solution. Just know that one isn't deterministic, so if anything on the website changes your program will break.
2

As @mike-scotty comented: "Use while"

It is recommended to use WebDriverWait and not time.sleep().

Should look something like this:

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

while WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".js-showMoreRecommends"))):
    show_more = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".js-showMoreRecommends")))
    show_more.click()

2 Comments

Thank you Moshe, this solution got stuck in a loop. Jose Martinez version (below) worked well, although it does not useWebDriverWait, but time.sleep.
@LuisMiguel Glad to hear! you Should try using WebDriverWait more... ;)
1

Insted of a while true loop you can try to while loop when the button is displayed

while driver.find_element_by_css_selector('.js-showMoreRecommends').is_displayed():
    driver.find_element_by_css_selector('.js-showMoreRecommends').click()
    time.sleep(2)

1 Comment

this will work fine! yet it's recommended to use WebDriverWait not time.sleep...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.