0

I am trying to get selenium to scroll down to a button in a website that has the text 'Try it out!' inside the button.

My problem is that there are no uniquely ID'd elements around the button to which I could scroll the view to. In addition, when I inspect the website with dev tools and search from the text 'Try it out!' in the HTML I get 72 results. I figured out that I need the 18th button but I am unable to get the browser to scroll to the button. Instead I get an error saying "The provided double value is non-finite".

Could you please look at the code below and give me an explanation to why I the browser is not scrolling down to the button?

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
import pathlib

# Get path to chromedriver
file_path = pathlib.Path(__file__).parent.absolute()
chromedriver_path = str(file_path)+"\\chromedriver.exe"

class Scraper:
    def __init__(self):
        
        # Open website
        self.driver = webdriver.Chrome(chromedriver_path)
        print(self.driver)
        self.driver.get(
            "https://flespi.io/gw/#/tags/!/devices/get_devices_dev_selector_messages")
        sleep(5)
        
        # Get the 18th button that says 'Try it out!'. Position()=17 because starts with 0.
        element = self.driver.find_element_by_xpath(
            '(//input[@value="Try it out!"])[position()=17]')
        
        # Scroll to the button and click it
        actions = ActionChains(self.driver)
        actions.move_to_element(element).perform()
        element.click()

        sleep(5)

Scraper()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
David
  • 15
  • 1
  • 7

3 Answers3

0

To grab the Try it out button and click on it first create a webdriver wait to wait for the element to be clickable.

element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='devices_get_devices_dev_selector_messages_content']/form/div[3]/input")))
element.click()

Import

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32
0

Please try the below code.

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

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
action = ActionChains(driver)

driver.get('https://flespi.io/gw/#/tags/!/devices/get_devices_dev_selector_messages')

Try_btn = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#devices_get_devices_dev_selector_messages_content > form > div.sandbox_header > input')))
action.move_to_element(Try_btn).click().perform()

This code works for me.

Swaroop Humane
  • 1,770
  • 1
  • 7
  • 17
0

This error message...

Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite

...implies that the WebDriver instance was unable to find the element for one or the other reasons:

  • The element haven't loaded properly when you tried to interact with it.
  • Element is within an <iframe> / <frame>
  • The style attribute of the element contains display: none;
  • Element is within an shadow DOM

You can find a relevant detailed discussion in javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite


This use-case

Among the 72 elements with text as Try it out! i.e.

<input class="submit" type="submit" value="Try it out!" data-sw-translate="">

barring the desired element all the other 71 elements have an ancestor with style attribute set as display: none; overflow: hidden;. Where as only the desired element is having style attribute set as overflow: hidden;.

to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#devices_get_devices_dev_selector_messages_content input[value='Try it out!']"))).click()
    
  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='devices_get_devices_dev_selector_messages_content']//input[@value='Try it out!']"))).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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352