Python 如何查找仅在使用Selenium滚动后可用的元素?

Python 如何查找仅在使用Selenium滚动后可用的元素?,python,python-3.x,selenium-webdriver,scroll,automation,Python,Python 3.x,Selenium Webdriver,Scroll,Automation,我想在页面上找到只有当用户滚动到它时才加载的元素。为了滚动到它,我必须找到它。但是,为了找到它,我必须滚动到它,以便它以html显示。有什么解决办法吗 我尝试通过xpath查找元素,以确保没有可查找的元素 channel\u text=driver.find\u element\u by_xpath(f”//*[contains(text(),“{name\u of_text\u inside\u tag}')]” 当我以开发人员模式打开页面并通过xpath进行搜索时,它发现没有。但是,当我滚动

我想在页面上找到只有当用户滚动到它时才加载的元素。为了滚动到它,我必须找到它。但是,为了找到它,我必须滚动到它,以便它以html显示。有什么解决办法吗

我尝试通过xpath查找元素,以确保没有可查找的元素

channel\u text=driver.find\u element\u by_xpath(f”//*[contains(text(),“{name\u of_text\u inside\u tag}')]”

当我以开发人员模式打开页面并通过xpath进行搜索时,它发现没有。但是,当我滚动到元素时,它通过非常相同的xpath找到它


非常感谢您的帮助。

您可以创建一个循环,滚动到页面底部并搜索元素

滚动之后正在加载元素,因此您需要等待它。这将使脚本速度变慢,但更可靠。创建具有可容忍时间延迟的等待对象。由于您可能需要滚动几次,超时前允许的时间应该很短。我建议您使用专门为此目的创建的等待对象,超时时间不超过5秒

您还需要设置一个限制,这样它就不会永远被关注

from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# create a dedicated wait object to wait for a brief period for the elements to be created.
wait = WebDriverWait(driver, 5)
# (...)
# find channel on SCROLLABLE side menu

# limit the number of scrolls
count = 0
while count < 5:
    driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
    try:
        channel_text = wait.until(
            EC.presence_of_element_located(
             (By.XPATH, f"//*[contains(text(), 'channel-name')]")
            )
        )
        break
    except TimeoutException:
        pass
    count += 1
else:
    # do whatever must be done if the element is never found.
    pass
print(channel_text)
selenium.common.Exception中的
导入NoTouchElementException、TimeoutException
从selenium.webdriver.common.by导入
从selenium.webdriver.support.ui导入WebDriverWait
从selenium.webdriver.support将预期的_条件导入为EC
#创建一个专用的wait对象,以便在短时间内等待要创建的元素。
wait=WebDriverWait(驱动程序,5)
# (...)
#在可滚动侧菜单上查找频道
#限制卷轴的数量
计数=0
当计数小于5时:
执行脚本(“window.scrollTo(0,document.body.scrollHeight);”)
尝试:
channel_text=等待(
EC.元素的存在位置(
(By.XPATH,f“/*[contains(text(),'channel name')]”)
)
)
打破
除TimeoutException外:
通过
计数+=1
其他:
#如果找不到元素,请执行必须执行的操作。
通过
打印(频道文本)

这对我很有效,试试这个


wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(“您的定位器”))

没有解决方法:您应该创建一个find-scroll\u a\u页面例程循环,并在查找时中断。创建一个滚动到页面底部的循环,然后检查元素。演示凭据?我不理解您试图通过
实现的目标!channel_tex
并且,如果第一次尝试时未找到元素,它将抛出异常,因此循环的其余部分无效。的目标是!通道\文本是在通道\文本不再为空时中断while。使用try-except块时,不需要!频道文字。它停止工作了吗?或者是因为它以前工作过,然后您更新了代码,但现在不再工作了?我在新文件中尝试了您的代码块,其中包含了selenium只需要的东西,但它根本找不到。问题是它不是随意滚动的,但它显然是可滚动的元素。问题是关于Selenium绑定的Python实现。答案看起来像是另一种编程语言的解决方案,不过可以翻译成Python。
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# create a dedicated wait object to wait for a brief period for the elements to be created.
wait = WebDriverWait(driver, 5)
# (...)
# find channel on SCROLLABLE side menu

# limit the number of scrolls
count = 0
while count < 5:
    driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
    try:
        channel_text = wait.until(
            EC.presence_of_element_located(
             (By.XPATH, f"//*[contains(text(), 'channel-name')]")
            )
        )
        break
    except TimeoutException:
        pass
    count += 1
else:
    # do whatever must be done if the element is never found.
    pass
print(channel_text)