I am working with a pre-defined field so you can only enter specific values and if you enter any other value (which is not mapped in database) then it throws error.
Now, I want to check for which values (from excel sheet), this field throws error but I am struggling to achieve this because the only things which changes in the DOM for invalid value is ::before. If it is invalid ::before will appear.
Example:
<label for="Broom" data-error="Please." class="active">::before</label>
When I came across this problem, I could not see any way of doing this through the WebDriver locator strategy like XPATH, CSS etc. We can only use this via Javascript. Something like this:
public String errorCheck() {
String script = "return window.getComputedStyle(document.querySelector('label[for=\\'Broom\\']'),':before').getPropertyValue('content')";
JavascriptExecutor js = (JavascriptExecutor)driver;
String content = (String) js.executeScript(script);
return content;
}
So, you can verify if it returns null for valid values. If you do more research around this, you will find you can do more validations as well i.e colour etc.
This is more from reference point of view.
Related
I have been trying to get the text from various div and tables for some BDD's I am writing in Cucumber. Everytime I try to acquire anything from the frontend of the website I'm working on it just pulls nothing. I need to know what I specifically need to do to get this content.
The page element I'm trying to retrieve from is similar to this
<div id="statMsg" class="statusMsg">Your changes are saved.</div>
These are the 2 methods that are currently trying to retrieve the text from the div.
public WebElement savedText() {
return webdriver.findElement(By.className("statusMsg"));
public void UserClicksOnSave() throws InterruptedException {
daSave.saveOn().click();
daPage.savedText().getText();
Find the DIV by XPATH such that it contains the text you are looking for:
public WebElement savedText() {
String xpath = "//div[contains(#class, 'statusMsg'][contains(., 'Your changes are saved.')]";
return webdriver.findElement(By.xpath(xpath));
Selenium should implicitly wait for the DIV matching that class name, and containing the given text. The findElement method should return only when both conditions are true, which should combat race conditions in your test.
I have a Java project using Selenium and Page Object Model and need to find buttons with IDs that end with the String "Cancel". I tried using regular expressions also, I found a few solutions on stackoverflow that included XPath, but taking into consideration that the website's design is changing often I do not use XPath.
I also found as a solution that you can use an ends-with CSS selector:
By.cssSelector("[id$=default-create-firstname]") but I would like to take advantage of the Page Object Model and use the annotation #FindBy, therefore omitting the By selectors.
#FindBy(id = "ButtonToCancel")
private WebElement buttonToCancel;
How can I select all the IDs in the page that end in *Cancel, without hardcoding each id find #FindBy? From what I know, Regex patterns do not work as such: #FindBy(id="*Cancel")
Try this:
#FindBy(css = "*[id$='cancel']"
private List<WebElement> cancelButtons;
It will return list of WebElements with id ended with "cancel" in id field.
I'm working with a DOM node:
<input
type="form-control"
type="text"
data-bind="textInput: EnterpriseId"
disabled
autocomplete="off">
How can I get its value? I'm struggling since element.getText() does not work and returns a blank.
Try this:
WebElement element = driver.findElement(By.id("id value"));
String val = element.getAttribute("innerText")
I presume the element in question is an <input> element, so you may be able to use the element.getAttribute(String attribute) method like so:
String value = element.getAttribute("value");
This input tag is disabled, hence element.getText() returns a blank value.
Use element.getAttribute("textContent") instead.
You may be looking for the placeholder of an input text, because you might try:
element.getAttribute("placeholder");
You can go to your browser → open developer tools → inspect element you want to take attribute from → click Properties → check if that value is in InnerText.
Then do as it is mentioned in previous comments:
element_locator.get_attribute('InnerText')
I had the exact same issue! This post solved it for me:
How can I get the current contents of an element in webdriver
I used:
element = driver.find_elements_by_xpath(
'//button[#class="size-grid-dropdown size-grid-button"]')
element.text
As other's suggested, HTML's input nodes don't have a text attribute because they can store data in multiple formats in a value attribute.
This can be easily seen in the HTML input API specification where this form control can be of type radio, date, file upload and many more.
So, in your specific case, I'd suggest you check the webdriver's API for a method that's able to retrieve the value attribute.
As a bonus to evaluate innerText of an element within Selenium:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("yourEl")));
wait.until(ExpectedConditions.attributeToBe(By.id("yourEl"), "innerText", yourValue));
Documentation: attributeToBe
It works definitely, as I've tested it several times:
<input type="form-control" type="text" data-bind="textInput: EnterpriseId" disabled autocomplete="off">
In your example, you don’t have any innerText. So you can only get attributes as mentioned before with the existing attributes. In your case:
type, data-bind, EnterpriseId and autocomplete. No value will be as this attribute isn’t created.
If you want to get only existing, this should be fine:
String example = driver.findElement(ByLocator(("")).getAttribute("any attribute of your input");
System.out.println(example);
I would like to get the value of all div tags specified in attached. I have tried with all possible locators like classname etc, which is showing null. and tried with JavaScript also which is returning null.
Please see the screen shot and I need the selected text which is in blue color starts with "Enables enterprise IT to deploy networking services"
You need to research creating selectors as this isn't a difficult one. There are numerous approaches for this element, but here's one for you: $$("#offers-popover .description"). Obviously this is a CSS selector based on the $$ and you use getText from the Selenium API in order to scrape the element text, which is what I assume you are intending to do.
driver.findElement(By.css("#offers-popover .description")).getText();
Since your element is not visible you can try this:
String divText = driver.findElement(By.className("description")).getAttribute("textContent");
Or, if this is not the only element on the page with the class description:
WebElement popElement = driver.findElement(By.id("offers-popover"));
String divText = popElement.findElement(By.className("description")).getAttribute("textContent");
I'm adding web tests to my project using Selenium. I already have a bunch of tests that check for a specific element using:
final WebElement dateElement = web.findElement(By.id(elementId));
And this works fine.
Now I have another requirement. This is in my generated page:
<input type="text" id="dateElement" name="dateElement" value="bunch of monkeys" tabindex="101" placeholder="yyyy-mm-dd">
<span class="error">dateElement is an invalid date</span>
How can I get hold of the error message?
I'd like something that allows me to request the span element with class "error" that is just after dateElement.
(This error message was ganerated by Spring MVC, so it's not easy to change it directly. Possible I guess, but I'd prefer not).
Any alternative idea is welcome.
OK, I already found a solution using Xpath and following-sibling, it wasn't too complicated.
final WebElement errorElement = web.findElement(By.xpath("//*[#id='" + elementId + "']/following-sibling::span[#class='error']"));
This gives me what I wanted, and throws a NoSuchElementException when it's not here, which is exactly what I want.
elementSelector = "input + span[class='error']";
final WebElement dateElement = web.findElement(By.cssSelector(elementSelector));