单击Selenium中的X按钮

发布于 2025-02-03 08:53:26 字数 514 浏览 2 评论 0 原文

这是我目前正在查看的网站的链接:。滚动到页面底部,然后单击“查看更多”。

我正在尝试弄清楚如何单击X按钮,但是我尝试过的方法尚未奏效。我得到一个“没有这样的元素:无法找到元素错误。

我尝试了所有这三个:

driver.find_element(By.CLASS_NAME, "button").click()

driver.find_element_by_xpath("//button[contains(@data-testid='CloseIcon')]").click()

driver.find_element_by_tag_name("svg").click()

Here is the link to the site I am currently viewing: https://messari.io/tool/fb8d86ca-d3cf-4568-8d48-1a052c95364e. Scroll to the bottom of the page and click "View More".

I am trying to figure out how to click the x button but what I have tried hasn't worked. I get a "no such element: Unable to locate element error.

I have tried all three of these:

driver.find_element(By.CLASS_NAME, "button").click()

driver.find_element_by_xpath("//button[contains(@data-testid='CloseIcon')]").click()

driver.find_element_by_tag_name("svg").click()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

垂暮老矣 2025-02-10 08:53:26

检查的XPATH加载更多按钮 //*[@ID =“ root”]/div [2]/div/div/div/div [2]/div [2]/div [3 ]/button -

​/I.SSTATIC.NET/SZ255.png“ rel =” nofollow noreferrer“>

x 按钮的完整XPath- '/html/html/hody/div [2]/div [3]/div/div/h2/button'

工作代码 -

import time

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

options = webdriver.ChromeOptions()

# options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920x1080")
options.add_argument("--disable-extensions")

chrome_driver = webdriver.Chrome(
    service=Service(ChromeDriverManager().install()),
    options=options
)


def messari_scraper():
    URL = "https://messari.io/tool/fb8d86ca-d3cf-4568-8d48-1a052c95364e"
    with chrome_driver as driver:
        driver.implicitly_wait(15)  # wait max 15 sec for any element to find
        driver.get(URL)
        time.sleep(3)
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")  # scroll to the end of the page

        # click the button
        driver.find_element(By.XPATH, '//*[@id="root"]/div[2]/div/div[2]/div[2]/div[3]/button').click()

        time.sleep(3)

        # get the full Xpath of the close `x` button of the pop-up
        driver.find_element(By.XPATH, '/html/body/div[2]/div[3]/div/h2/button').click()
        # pop up window closed

        time.sleep(5)

        # do your tasks here....


messari_scraper()

Check the Xpath of the load more button //*[@id="root"]/div[2]/div/div[2]/div[2]/div[3]/button -

messari load more button xpath

And then check the full Xpath of the X button that will close the pop-up window

messari X button to close pop-up full xpath

full Xpath of the X button - '/html/body/div[2]/div[3]/div/h2/button'

working code -

import time

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

options = webdriver.ChromeOptions()

# options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920x1080")
options.add_argument("--disable-extensions")

chrome_driver = webdriver.Chrome(
    service=Service(ChromeDriverManager().install()),
    options=options
)


def messari_scraper():
    URL = "https://messari.io/tool/fb8d86ca-d3cf-4568-8d48-1a052c95364e"
    with chrome_driver as driver:
        driver.implicitly_wait(15)  # wait max 15 sec for any element to find
        driver.get(URL)
        time.sleep(3)
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")  # scroll to the end of the page

        # click the button
        driver.find_element(By.XPATH, '//*[@id="root"]/div[2]/div/div[2]/div[2]/div[3]/button').click()

        time.sleep(3)

        # get the full Xpath of the close `x` button of the pop-up
        driver.find_element(By.XPATH, '/html/body/div[2]/div[3]/div/h2/button').click()
        # pop up window closed

        time.sleep(5)

        # do your tasks here....


messari_scraper()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文