i am trying to display the number 1 in my console, apparently it did not work, any advise please
This is my code apparently it didn't work.
String error1 = driver.findElement(By.xpath("//div[#class='label label-error']/span[#class='sidebar-label']")).getText();
the console error output
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class='label label-error']/span[#class='sidebar-label']"}
Your Xpath is Wrong, Use one of these:
//div[#class="sidebar-label"]/span
//div[#class="sidebar-label"]/span[#class="label label-error"]
//*[#class="label label-error"]
Related
I have following xpaths, which should be handled on same way by WebDriver, I need to get text content from these.
//*[#id="dialogMessage"]/div[3]
//*[#id="dialogMessage"]/div[3]/p
//*[#id="dialogMessage"]/div[3]/p/span[2]
I tried to use this code to match all of the above ones.
String result_text = driver.findElement(By.xpath("//*[contains(#value, 'dialogMessage')]")).getText();
Only one of these xpaths is present on page in each page loads. I get following error message:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//id[contains(#value, 'dialogMessage')]"}
Looks like you're trying to get all elements that contain attribute value equals to dialogMessage instead try using this xpath:
//*[contains(#id, 'dialogMessage')]
or
//*[#id='dialogMessage']
Please see below attached the relevant XML code and my java code to locate the element:
Java code:
//Set date range
WebElement parentStartdate = obj.findElement(By.xpath("//[#id=typein_[Parameters].[Parameter 3]]"));
WebElement child=parentStartdate.findElement(By.xpath("//input[#value='1/1/2019']"));
I get the follow error:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //[#id=typein_[Parameters].[Parameter 3]] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//[#id=typein_[Parameters].[Parameter 3]]' is not a valid XPath expression.
Any help appreciated!
The whole xml structure:
If you have only one QueryBox on the webpage then you can select the element by using the following xpath for your case:
WebElement element = driver.findElement(By.xpath("//input[#class='QueryBox']"));
As the error message says, that is not a valid Xpath expression. Select the first element by
WebElement parentStartdate = obj.findElement(By.xpath("//span[#class='TypeInQuerySpan']/input"));
Try this selector:
//div[contains(#class, 'TypeInDiv')]/span[#class="TypeInQuerySpan"]/input[#type='text'][#class='QueryBox'][#value='1/1/2019']
the 3 identifiers on input are not mandatory, you can choose 1 or 2 or leave all 3, you decide.
Inspect element from Google Chrome:
<div class="form-style-abc">
<fieldset>
<legend><span class="number">*</span>Search Zone</legend>
Need to retrieve the "Search Zone", however im unable to perform the search and getText()
I had performed the following on Eclipse but getting error:
Code:
String HeaderTxt = driver.findElement(By.xpath("//span[#class = 'number']")).getText();
System.out.println(HeaderTxt);
Error Message:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//span[#class = 'number']"}
As per the HTML you have provided to extract the text Search Zone you can use the following line of code :
String HeaderTxt = driver.findElement(By.xpath("//div[#class='form-style-abc']/fieldset/legend[(self::legend) and not (#class='number')]")).getText();
System.out.println(HeaderTxt);
Update 1
As per your comment update as you are still seeing NoSuchElementException possibly you need to wait for the element inducing WebDriverWait as follows :
WebElement HeaderTxtElem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='form-style-abc']/fieldset/legend[(self::legend) and not (#class='number')]")));
System.out.println(HeaderTxtElem.getText());
Update 2
As you mentioned the element is within an <iframe> tag you need to switch to the exact frame and then look out for the WebElement. Here you can find a detailed discussion on How to switch to frame in Selenium Webdriver Java
Update 3
Xpath Explanation
Through the xpath as "//div[#class='form-style-abc']/fieldset/legend" we have reached till the <legend> node. Now you want to get the text Search Zone only. So being within the <legend> tag you have to trim the child <span> tag which have a class as number. So we added a clause within our xpath not to consider (#class='number') while retrieving the text as in :
String HeaderTxt = driver.findElement(By.xpath("//div[#class='form-style-abc']/fieldset/legend[(self::legend) and not (#class='number')]")).getText();
I'm having a strange problem. sendkeys doesn't work on an input field although I am able to do a click on it.
I also tried setAttribute and even that doesn't work.
The input field is constantly monitoring user input and triggers a javascript when the user enters x number of characters. Is this causing the problem?
I tried these:
driver.findElement(By.xpath("//input[#id='id']")).sendKeys(codeStr);
((JavascriptExecutor) driver).executeScript("document.getElementById('id').value='
123456'");
((JavascriptExecutor)
driver).executeScript("document.getElementById('id').
setAttribute('value', '123456')");
This is the error I receive:
org.openqa.selenium.NoSuchElementException: Unable to locate element: //input[#id='id']
but the click works fine:
driver.findElement(By.xpath("//input[#id='id']")).click();
I am new to Automation, and trying to automate a website. I was able to login to the site using webdriver, however I am unable to locate any elements on the next page.
I have tried locating it by id, name, css selector, linktext, xpath etc. I even put an explicit wait but that was of no help either.
I get the following error every time I try to locate the element:
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"Owner to Channel "}
Here is the page source detail for the above mentioned button:
Page source for the button
Code to locate the button:
WebElement el = driver.findElement(By.linkText("Owner to Channel "));
el.click();
Any suggestions? May be I am missing something.
Thank you all for your suggestions.
I found that there was a frame in the page source, but it didn't have an id or name.
By hit and trial, I tried switching to the frame with id 0, and it worked!