0

I'm trying to click on see more (se flere) on this particular website using selenium and python. This is how I am testing it, and i get the following error

driver.find_element_by_css_selector('[class="button button--primary style_loadMore__2rYaL style_button__xicB7"]').click()

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted:

This is what works:

tresults = driver.find_elements_by_xpath('//button[text()="Se flere"]') #flere"]')
for idx, tr in enumerate(tresults):
    #print (tr.text, tr.get_attribute('class'))
    if tr.get_attribute('class') == 'button button--primary style_loadMore__2rYaL style_button__xicB7':
        driver.execute_script("arguments[0].click();", tr)
        #tr.click()

What am I missing?

tandem
  • 2,040
  • 4
  • 25
  • 52
  • 1
    Does this answer your question? [Can not click on a Element: ElementClickInterceptedException in Splinter / Selenium](https://stackoverflow.com/questions/48665001/can-not-click-on-a-element-elementclickinterceptedexception-in-splinter-selen) –  Jun 17 '20 at 20:24

2 Answers2

0

To click on Se flere you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get('https://coop.no/sortiment/obs-bygg/hageuterom/hytter-boder-og-drivhus')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='CategoryPage']"))).click()
    
  • Using XPATH:

    driver.get('https://coop.no/sortiment/obs-bygg/hageuterom/hytter-boder-og-drivhus')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@type='CategoryPage']"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

coop


Reference

You can find a couple of detailed discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • This does not seem to work, and gives the same error. – tandem Jun 17 '20 at 20:30
  • @tandem Checkout the updated answer and let me know the status. – undetected Selenium Jun 17 '20 at 20:33
  • Yes, it works. apologies. I had a sleep timer inside the block, and I found this in one of your prior answers. "While using Selenium for automation using time.sleep(secs) without any specific condition to achieve defeats the purpose of automation and should be avoided at any cost. As per the documentation:" – tandem Jun 17 '20 at 20:41
  • This does solve the problem of rendering new elements on the screen. However, when I try to get elements from the product-card, I only get elements that were originally on the website before rendering new elements. – tandem Jun 17 '20 at 20:53
  • @tandem That all together sounds like a new question. Can you raise a new question as per your new requirement please? – undetected Selenium Jun 17 '20 at 20:54
  • Maybe I can rephrase: With the above code, I'm unable to render the products using selenium. In that aspect, I view it as a same question. but if you think it is different, it's ok – tandem Jun 17 '20 at 20:58
  • Let me know if there is anything else I can provide – tandem Jun 17 '20 at 21:07
0

Here is a very simple running code snippet that uses XPATH to find the button Text "Se flere".

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://coop.no/sortiment/obs-bygg/hageuterom/hytter-boder-og- 
drivhus")
element = driver.find_element_by_xpath('//button[text()="Se flere"]')
element.click()
Adnan Temur
  • 333
  • 1
  • 10
  • `selenium.common.exceptions.NoSuchElementException:` that's the error i get – tandem Jun 17 '20 at 20:31
  • see the updated answer – Adnan Temur Jun 17 '20 at 21:17
  • This does solve the problem of rendering new elements on the screen. However, when I try to get elements from the product-card, I only get elements that were originally on the website before rendering new elements. – tandem Jun 17 '20 at 21:17
  • Your stated problem in above comment is unclear. Selenium is just to interact with the website not to alter its database! – Adnan Temur Jun 17 '20 at 21:21
  • I understand that Adnan. Let's take an example, if you scroll this webpage, you see a stream of new products. I was hoping to see these when I iterate over the product card ID. Initially, you see 32 products. After the first scroll, I was hoping to see 64. – tandem Jun 17 '20 at 21:28
  • When I run this, I see all products all the way down. You just have to wait a few seconds.The last product I see is: Palmako Nordic+ 1/4 vinduselement 3 790 – Adnan Temur Jun 17 '20 at 21:39
  • I think I see what you mean. I'll give it a shot – tandem Jun 17 '20 at 21:47