Webdriver search within element - java

In Telerik it’s possible to search elements within an element that was already found. E.g. I found an ul, that has some elements li. After that I can invoke find() directly from the element.
Is there such possibility using the WebDriver Java?

In WebDriver the usual way of finding an element on the page is;
WebElement element = driver.findElement(By.xpath("xpath query here"));
The findElement method is provided by SearchContext inferface, which WebElement also extends. This means you can call findElement on any element found by a previous search;
WebElement child = element.findElement(By.xpath("another xpath query"));

Actually, you can use
List<WebElement> = driver.findElements(By.cssSelector(".ul li"));
Your list contains all li elements who are in the ul.

Related

How to Print all the Search Results Names Using Pagination in amazon? using selenium and java [duplicate]

i would like to find my TextField in selenium, but i dont know how (i use sellenium for the first time).
I tried:
driver.findElement(By.id("originTextField"))
or by xPath and cssSelector String generated by chrome in dev tools.
Please help me, i would appreciate explanation.
this is html:
NoSuchElementException
org.openqa.selenium.NoSuchElementException popularly known as NoSuchElementException extends org.openqa.selenium.NotFoundException which is a type of WebDriverException.
NoSuchElementException can be thrown in 2 cases as follows :
When using WebDriver.findElement(By by) :
//example : WebElement my_element = driver.findElement(By.xpath("//my_xpath"));
When using WebElement.findElement(By by) :
//example : WebElement my_element = element.findElement(By.xpath("//my_xpath"));
As per the JavaDocs just like any other WebDriverException, NoSuchElementException should contain the following Constant Fields :
Constant Field Type Value
SESSION_ID public static final java.lang.String "Session ID"
e.g. (Session info: chrome=63.0.3239.108)
DRIVER_INFO public static final java.lang.String "Driver info"
e.g. (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86)
BASE_SUPPORT_URL protected static final java.lang.String "http://seleniumhq.org/exceptions/"
e.g. (For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html)
Reason
The reason for NoSuchElementException can be either of the following :
The Locator Strategy you have adopted doesn't identifies any element in the HTML DOM.
The Locator Strategy you have adopted is unable to identify the element as it is not within the browser's Viewport.
The Locator Strategy you have adopted identifies the element but is invisible due to presence of the attribute style="display: none;".
The Locator Strategy you have adopted doesn't uniquely identifies the desired element in the HTML DOM and currently finds some other hidden / invisible element.
The WebElement you are trying to locate is within an <iframe> tag.
The WebDriver instance is looking out for the WebElement even before the element is present/visibile within the HTML DOM.
Solution
The solution to address NoSuchElementException can be either of the following :
Adopt a Locator Strategy which uniquely identifies the desired WebElement. You can take help of the Developer Tools (Ctrl+Shift+I or F12) and use Element Inspector.
Here you will find a detailed discussion on how to inspect element in selenium3.6 as firebug is not an option any more for FF 56?
Use executeScript() method to scroll the element in to view as follows :
WebElement elem = driver.findElement(By.xpath("element_xpath"));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", elem);
Here you will find a detailed discussion on Scrolling to top of the page in Python using Selenium
Incase element is having the attribute style="display: none;", remove the attribute through executeScript() method as follows :
WebElement element = driver.findElement(By.xpath("element_xpath"));
((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('style')", element)
element.sendKeys("text_to_send");
To check if the element is within an <iframe> traverse up the HTML to locate the respective <iframe> tag and switchTo() the desired iframe through either of the following methods :
driver.switchTo().frame("frame_name");
driver.switchTo().frame("frame_id");
driver.switchTo().frame(1); // 1 represents frame index
Here you can find a detailed discussion on Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?.
If the element is not present/visible in the HTML DOM immediately, induce WebDriverWait with ExpectedConditions set to proper method as follows :
To wait for presenceOfElementLocated :
new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[#class='buttonStyle']//input[#id='originTextField']")));
To wait for visibilityOfElementLocated :
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='buttonStyle']//input[#id='originTextField']")));
To wait for elementToBeClickable :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='buttonStyle']//input[#id='originTextField']")));
Reference
You can find Selenium's python client based relevant discussion in:
Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
Your code is correct, I suspect the issue caused the page not complete load when you find the element.
Try add a long sleep before find element, if adding sleep worked, change sleep to wait.
Here is the code, It means waiting 10s if the element isn’t present:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "originTextField"))
)
Below code will helps you get it resolved
wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("originTextField")));
or
wait = new WebDriverWait(driver, 90);
wait.until(ExpectedConditions.visibilityOf(element)).wait(20);

How do i click this element via className since i will be using it for multiple screens with same class name

The element I got is <div class="x-tool x-box-item" id="tool-123" src="data:image/gif;base64" class="new-img x-tool-maximize".
I particularly need this class="new-img x-tool-maximize" because its the common of all the screen.
I already tried
driver.findElement(By.className("new-img.x-tool-maximize")).click()
and
driver.findElement(By.className("new-img x-tool-maximize")).click();
and
driver.findElement(By.xpath("//div[contains(#class, 'value') and contains(#class, 'test')]"))``;
You should be able to use a CSS selector to find this type of element. You’ll want something like driver.findElement(By.cssSelector("div.new-img.x-tool-maximize")).
You can use list in selenium if you have multiple elements with same locators
List<WebElement> classes=driver.findElements(By.classname("new-img x-tool-maximize));
// if you want click 1 st class name element use this following line
classes.get(0).click();
OR // if you want click 2 nd class name element use this following line
classes.get(1).click();
Use a list to get all the WebElements with specific classes
List<WebElement> list = driver.findElements(By.cssSelector("div.new-img.x-tool-maximize"));
As you intent to click() on the element using only the className attribute values, ideally you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector using only the className attribute values:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector(".new-img.x-tool-maximize"))).click();
For a more canonicl approach you can indlude the tagName as follows:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.new-img.x-tool-maximize]"))).click();
References
You can find a couple of relevant detailed discussions in:
Unable to locate element using className in Selenium and Java
How to locate the last web element using classname attribute through Selenium and Python
What are properties of find_element_by_class_name in selenium python?

Unable to identify elements in a specific page, using Selenium WebDriver with Java [duplicate]

i would like to find my TextField in selenium, but i dont know how (i use sellenium for the first time).
I tried:
driver.findElement(By.id("originTextField"))
or by xPath and cssSelector String generated by chrome in dev tools.
Please help me, i would appreciate explanation.
this is html:
NoSuchElementException
org.openqa.selenium.NoSuchElementException popularly known as NoSuchElementException extends org.openqa.selenium.NotFoundException which is a type of WebDriverException.
NoSuchElementException can be thrown in 2 cases as follows :
When using WebDriver.findElement(By by) :
//example : WebElement my_element = driver.findElement(By.xpath("//my_xpath"));
When using WebElement.findElement(By by) :
//example : WebElement my_element = element.findElement(By.xpath("//my_xpath"));
As per the JavaDocs just like any other WebDriverException, NoSuchElementException should contain the following Constant Fields :
Constant Field Type Value
SESSION_ID public static final java.lang.String "Session ID"
e.g. (Session info: chrome=63.0.3239.108)
DRIVER_INFO public static final java.lang.String "Driver info"
e.g. (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86)
BASE_SUPPORT_URL protected static final java.lang.String "http://seleniumhq.org/exceptions/"
e.g. (For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html)
Reason
The reason for NoSuchElementException can be either of the following :
The Locator Strategy you have adopted doesn't identifies any element in the HTML DOM.
The Locator Strategy you have adopted is unable to identify the element as it is not within the browser's Viewport.
The Locator Strategy you have adopted identifies the element but is invisible due to presence of the attribute style="display: none;".
The Locator Strategy you have adopted doesn't uniquely identifies the desired element in the HTML DOM and currently finds some other hidden / invisible element.
The WebElement you are trying to locate is within an <iframe> tag.
The WebDriver instance is looking out for the WebElement even before the element is present/visibile within the HTML DOM.
Solution
The solution to address NoSuchElementException can be either of the following :
Adopt a Locator Strategy which uniquely identifies the desired WebElement. You can take help of the Developer Tools (Ctrl+Shift+I or F12) and use Element Inspector.
Here you will find a detailed discussion on how to inspect element in selenium3.6 as firebug is not an option any more for FF 56?
Use executeScript() method to scroll the element in to view as follows :
WebElement elem = driver.findElement(By.xpath("element_xpath"));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", elem);
Here you will find a detailed discussion on Scrolling to top of the page in Python using Selenium
Incase element is having the attribute style="display: none;", remove the attribute through executeScript() method as follows :
WebElement element = driver.findElement(By.xpath("element_xpath"));
((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('style')", element)
element.sendKeys("text_to_send");
To check if the element is within an <iframe> traverse up the HTML to locate the respective <iframe> tag and switchTo() the desired iframe through either of the following methods :
driver.switchTo().frame("frame_name");
driver.switchTo().frame("frame_id");
driver.switchTo().frame(1); // 1 represents frame index
Here you can find a detailed discussion on Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?.
If the element is not present/visible in the HTML DOM immediately, induce WebDriverWait with ExpectedConditions set to proper method as follows :
To wait for presenceOfElementLocated :
new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[#class='buttonStyle']//input[#id='originTextField']")));
To wait for visibilityOfElementLocated :
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='buttonStyle']//input[#id='originTextField']")));
To wait for elementToBeClickable :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='buttonStyle']//input[#id='originTextField']")));
Reference
You can find Selenium's python client based relevant discussion in:
Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
Your code is correct, I suspect the issue caused the page not complete load when you find the element.
Try add a long sleep before find element, if adding sleep worked, change sleep to wait.
Here is the code, It means waiting 10s if the element isn’t present:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "originTextField"))
)
Below code will helps you get it resolved
wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("originTextField")));
or
wait = new WebDriverWait(driver, 90);
wait.until(ExpectedConditions.visibilityOf(element)).wait(20);

Unable to perform click or sendkeys using htmlunit driver [duplicate]

i would like to find my TextField in selenium, but i dont know how (i use sellenium for the first time).
I tried:
driver.findElement(By.id("originTextField"))
or by xPath and cssSelector String generated by chrome in dev tools.
Please help me, i would appreciate explanation.
this is html:
NoSuchElementException
org.openqa.selenium.NoSuchElementException popularly known as NoSuchElementException extends org.openqa.selenium.NotFoundException which is a type of WebDriverException.
NoSuchElementException can be thrown in 2 cases as follows :
When using WebDriver.findElement(By by) :
//example : WebElement my_element = driver.findElement(By.xpath("//my_xpath"));
When using WebElement.findElement(By by) :
//example : WebElement my_element = element.findElement(By.xpath("//my_xpath"));
As per the JavaDocs just like any other WebDriverException, NoSuchElementException should contain the following Constant Fields :
Constant Field Type Value
SESSION_ID public static final java.lang.String "Session ID"
e.g. (Session info: chrome=63.0.3239.108)
DRIVER_INFO public static final java.lang.String "Driver info"
e.g. (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86)
BASE_SUPPORT_URL protected static final java.lang.String "http://seleniumhq.org/exceptions/"
e.g. (For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html)
Reason
The reason for NoSuchElementException can be either of the following :
The Locator Strategy you have adopted doesn't identifies any element in the HTML DOM.
The Locator Strategy you have adopted is unable to identify the element as it is not within the browser's Viewport.
The Locator Strategy you have adopted identifies the element but is invisible due to presence of the attribute style="display: none;".
The Locator Strategy you have adopted doesn't uniquely identifies the desired element in the HTML DOM and currently finds some other hidden / invisible element.
The WebElement you are trying to locate is within an <iframe> tag.
The WebDriver instance is looking out for the WebElement even before the element is present/visibile within the HTML DOM.
Solution
The solution to address NoSuchElementException can be either of the following :
Adopt a Locator Strategy which uniquely identifies the desired WebElement. You can take help of the Developer Tools (Ctrl+Shift+I or F12) and use Element Inspector.
Here you will find a detailed discussion on how to inspect element in selenium3.6 as firebug is not an option any more for FF 56?
Use executeScript() method to scroll the element in to view as follows :
WebElement elem = driver.findElement(By.xpath("element_xpath"));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", elem);
Here you will find a detailed discussion on Scrolling to top of the page in Python using Selenium
Incase element is having the attribute style="display: none;", remove the attribute through executeScript() method as follows :
WebElement element = driver.findElement(By.xpath("element_xpath"));
((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('style')", element)
element.sendKeys("text_to_send");
To check if the element is within an <iframe> traverse up the HTML to locate the respective <iframe> tag and switchTo() the desired iframe through either of the following methods :
driver.switchTo().frame("frame_name");
driver.switchTo().frame("frame_id");
driver.switchTo().frame(1); // 1 represents frame index
Here you can find a detailed discussion on Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?.
If the element is not present/visible in the HTML DOM immediately, induce WebDriverWait with ExpectedConditions set to proper method as follows :
To wait for presenceOfElementLocated :
new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[#class='buttonStyle']//input[#id='originTextField']")));
To wait for visibilityOfElementLocated :
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='buttonStyle']//input[#id='originTextField']")));
To wait for elementToBeClickable :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='buttonStyle']//input[#id='originTextField']")));
Reference
You can find Selenium's python client based relevant discussion in:
Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
Your code is correct, I suspect the issue caused the page not complete load when you find the element.
Try add a long sleep before find element, if adding sleep worked, change sleep to wait.
Here is the code, It means waiting 10s if the element isn’t present:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "originTextField"))
)
Below code will helps you get it resolved
wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("originTextField")));
or
wait = new WebDriverWait(driver, 90);
wait.until(ExpectedConditions.visibilityOf(element)).wait(20);

WebDriver PageFactory Find Elements List

I have multiple elements on a page and I would like to initialize them using PageFactory.
I have tried using following
#FindBy(xpath = "//*[contains(#class,'x-grid-tree-node-leaf')]")
List<WebElement> allElements;
but this returns only one element.
now, if I use the traditional way for finding elements
List<WebElement> allElements = driver.findElements(By.xpath("//*[contains(#class,'x-grid-tree-node-leaf')]"));
this returns 4 elements
any pointers what could be the issue?
#FindBy(xpath = "//*[contains(#class,'x-grid-tree-node-leaf')]")
List<WebElement> allElements;
this works. there was bug in my code.
Use FindAll annotation to get series of #FindBy tags and search for all elements that match any of the FindBy criteria.
#FindAll(#FindBy(how = How.XPATH, using = "//*[contains(#class,'x-grid-tree-node-leaf')]"))
List<WebElement> allElements;
Instead of using #FindBy annotation, use #FindAllBy annotation.Try this!
#FindAllBy(xpath = "//*[contains(#class,'x-grid-tree-node-leaf')]")
List<WebElement> allElements;
Here's the link for FindAllBy java class.
Have you tried running your xpath in Chrome Developer tool or in Firebug?
List<WebElement> allElements = driver.findElements(By.xpath("//*[contains(#class,'x-grid-tree-node-leaf')]"));
should work.

Categories

Resources