Home > OS >  How to click on a menu with svg tag on it using selenium
How to click on a menu with svg tag on it using selenium

Time:01-25

I want to click on a menu button using selenium, but isn't working using WebDriverWait or any other way. How can i click on it using selenium from https://www.dappradar.com/binance-smart-chain/defi/drip here

<div  title="Menu"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0V0z"></path><path d="M3 18h18v-2H3v2zm0-5h18v-2H3v2zm0-7v2h18V6H3z"></path></svg></div>

CodePudding user response:

SVG is wrapped inside div tag, so you can either locate SVG or div.

I can do it with div:

driver.maximize_window()
wait = WebDriverWait(driver, 30)

driver.get("https://dappradar.com/binance-smart-chain/defi/drip")

try:
    wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@title='Close']"))).click()
    print('Clicked on closed icon')
except:
    pass

wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@title='Menu']"))).click()

Imports:

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

CodePudding user response:

I'm doing it in Java, so what I do is first I scroll to particular location where our desired element exist (somewhere in the bottom). then apply implicit wait so our desired element is ready to be clickable then click on it.

        WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(100));
        driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(50));
        driver.get("https://dappradar.com/binance-smart-chain/defi/drip");
        driver.manage().window().maximize();

        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollBy(0,350)", "");

        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div[title='Menu']")));
        System.out.println("Button enabled:  "   driver.findElement(By.cssSelector("div[title='Menu']")).isDisplayed());
        driver.findElement(By.cssSelector("div[title='Menu']")).click();

        wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div[class='apexcharts-menu apexcharts-menu-open']")));

        List<WebElement> elements = driver.findElements(By.cssSelector("div[class='apexcharts-menu apexcharts-menu-open'] > div"));

        System.out.println("List size  "   elements.size());
  •  Tags:  
  • Related