Search code examples
javaseleniumwebdriverwaitoperatradingview-api

Can't find web element with Selenium Java within tradingview.com


I can't access a specific element of a webpage with Selenium in Java.

I need to access the "sign in" option inside of a submenu of the Trading View webpage.

I tried xPath, CSS selector, etc. but nothing works, I guess it's inside some kind of subdivision of the webpage I don't know, but I'm positive it's not an <iframe>, I have already checked that. I could be wrong though.

The element I need to access

My code:

public class Main {
  public static void main(String[] args){

      System.setProperty("webdriver.opera.driver","C:\\Documents\\LIBRARIES\\Opera Driver\\operadriver.exe");
      WebDriver driver = new OperaDriver();
      driver.get("https://www.tradingview.com");
      driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(4));
      driver.findElement(By.xpath(("/html/body/div[2]/div[3]/div[2]/div[3]/button[1]"))).click();
      driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(4));
     // This is where I need to select the element of the webpage, nothing works so far (Xpath, CSS, ID...)
  }
}

Solution

  • To click on the Sign in element first you need to click on the user menu icon and then you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:

    • Using cssSelector:

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[aria-label='Open user menu']"))).click();
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div[data-name='header-user-menu-sign-in'] div[class^='label'] div[class^='label']"))).click();
      
    • Using xpath:

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@aria-label='Open user menu']"))).click();
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[text()='Sign in']"))).click();