"The provided double value is non-finite" 尝试使用 Selenium 和 Python 定位元素时出错
"The provided double value is non-finite" error when attempting to get the locate an element using Selenium and Python
我试图让 selenium 向下滚动到网站中的按钮,该按钮内有文本 'Try it out!'。
我的问题是按钮周围没有我可以将视图滚动到的唯一 ID 元素。此外,当我使用开发工具检查网站并从 HTML 中的文本 'Try it out!' 进行搜索时,我得到了 72 个结果。我发现我需要第 18 个按钮,但我无法让浏览器滚动到该按钮。相反,我收到一条错误消息“提供的双精度值是非有限的”。
你能看看下面的代码并解释为什么我的浏览器没有向下滚动到按钮吗?
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()
要抓住试用按钮并单击它,首先创建一个 webdriver wait 等待元素可单击。
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()
导入
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
请尝试以下代码。
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()
这段代码适合我。
这个错误信息...
Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite
...表示 实例由于一个或另一个原因无法找到元素:
- 当您尝试与之交互时,该元素未正确加载。
- 元素在
<iframe>
/ <frame>
内
- 元素的样式属性包含
display: none;
- 元素在 阴影内 DOM
You can find a relevant detailed discussion in
这个use-case
在 72 个元素中,文本为 试试吧! 即
<input class="submit" type="submit" value="Try it out!" data-sw-translate="">
除所需元素外,所有其他 71 个元素的祖先属性 style
设置为 display: none; overflow: hidden;
。只有所需的元素将 style
属性设置为 overflow: hidden;
.
点击需要诱导的元素 for the element_to_be_clickable()
and you can use either of the following :
使用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()
使用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()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
我试图让 selenium 向下滚动到网站中的按钮,该按钮内有文本 'Try it out!'。
我的问题是按钮周围没有我可以将视图滚动到的唯一 ID 元素。此外,当我使用开发工具检查网站并从 HTML 中的文本 'Try it out!' 进行搜索时,我得到了 72 个结果。我发现我需要第 18 个按钮,但我无法让浏览器滚动到该按钮。相反,我收到一条错误消息“提供的双精度值是非有限的”。
你能看看下面的代码并解释为什么我的浏览器没有向下滚动到按钮吗?
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()
要抓住试用按钮并单击它,首先创建一个 webdriver wait 等待元素可单击。
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()
导入
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
请尝试以下代码。
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()
这段代码适合我。
这个错误信息...
Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite
...表示
- 当您尝试与之交互时,该元素未正确加载。
- 元素在
<iframe>
/<frame>
内
- 元素的样式属性包含
display: none;
- 元素在 阴影内 DOM
You can find a relevant detailed discussion in
这个use-case
在 72 个元素中,文本为 试试吧! 即
<input class="submit" type="submit" value="Try it out!" data-sw-translate="">
除所需元素外,所有其他 71 个元素的祖先属性 style
设置为 display: none; overflow: hidden;
。只有所需的元素将 style
属性设置为 overflow: hidden;
.
点击需要诱导的元素element_to_be_clickable()
and you can use either of the following
使用
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()
使用
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()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC