0

I'm new in Selenium and WebDriver. I have this HTML:

<input id="undefined-undefined-Jobsubject-5546" type="text" value="" data-test="testing-job-subject" style="padding: 0px; position: relative; width: 100%; border: medium none; outline: medium none; background-color: transparent; color: rgb(255, 255, 255); cursor: initial; font: inherit; height: 100%; box-sizing: border-box; margin-top: 14px;"/>

and I have this code:

driver.findElement(By.xpath("//input[@data-test='testing-job-subject']"));

but the error is:

org.openqa.selenium.NoSuchElementException: Unable to locate element: //input[@data-test='testing-job-subject']

I tried also this:

driver.findElement(By.xpath("//*[starts-with(@id,'undefined-undefined-Jobsubject')]"));

because the number in id is generated, so I can't take the By.id(....), but the same error. And yes,I have in the code the timeouts,so the element is on the page.

Where is the problem? Thanks

Mephy
  • 3
  • 1
  • 2

3 Answers3

2

If you are getting NoSuchElementException as your provided exception, There may be following reasons :-

  • May be when you are going to find element, it would not be present on the DOM, So you should implement WebDriverWait to wait until element visible as below :-

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[data-test='testing-job-subject']")));
    
  • May be this element is inside any frame or iframe. If it is, you need to switch that frame or iframe before finding the element as below :-

    WebDriverWait wait = new WebDriverWait(driver, 10);
    
    //Find frame or iframe and switch
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("your frame id or name"));
    
    //Now find the element 
    WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[data-test='testing-job-subject']")));
    
    //Once all your stuff done with this frame need to switch back to default
    driver.switchTo().defaultContent();
    
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • I tried the first reason, but still nothing, just another error: "org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.cssSelector: input[data-test='testing-job-subject'] (tried for 10 second(s) with 500 MILLISECONDS interval)" And there is no frame or iframe :/ – Mephy Sep 07 '16 at 17:00
  • So how to be ensure you that there is no frame or iframe?? – Saurabh Gaur Sep 07 '16 at 18:24
  • @Mephy Are you sure you have provided here right HTML code for the link element?? and also make sure this element is manually visible on the page.. – Saurabh Gaur Sep 07 '16 at 18:27
0

Try this:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[data-test='testing-job-subject']")));
Ranjith's
  • 4,508
  • 5
  • 24
  • 40
0

This should work for you.

driver.findElement(By.cssSelector("id*='Jobsubject'"));
Pang
  • 9,564
  • 146
  • 81
  • 122