单击 selenium 中的 x 按钮
Clicking an x button in selenium
这是我当前正在查看的网站 link:https://messari.io/tool/fb8d86ca-d3cf-4568-8d48-1a052c95364e。滚动到页面底部并点击“查看更多”。
我正在尝试弄清楚如何单击 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()
检查 load more
按钮的 Xpath //*[@id="root"]/div[2]/div/div[2]/div[2]/div[3]/button
-
然后检查将关闭 pop-up window
的 X
按钮的完整 Xpath
X
按钮的完整 Xpath - '/html/body/div[2]/div[3]/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()
这是我当前正在查看的网站 link:https://messari.io/tool/fb8d86ca-d3cf-4568-8d48-1a052c95364e。滚动到页面底部并点击“查看更多”。
我正在尝试弄清楚如何单击 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()
检查 load more
按钮的 Xpath //*[@id="root"]/div[2]/div/div[2]/div[2]/div[3]/button
-
然后检查将关闭 pop-up window
的X
按钮的完整 Xpath
X
按钮的完整 Xpath - '/html/body/div[2]/div[3]/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()