Robot Framework: get element by text - selenium

I'm testing a website which is not exactly designed for automation testing. I'm using Robot Framework, Selenium2Library. There's an element that I cannot locate it by x-path the web driver will always locate to another element. Is there anyway to get the element by its text ?
The example HTML looks like
<label>Next Steps</label>
Is there any additional library that allows something like
Get Element By Text Next Steps
Please suggest.

You can get that element by text using xPath as well:
//label[text()="Next Steps"]

You could use
Wait Until Element Is Visible //label[.='Next Steps']
Recommended pages:
https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html
https://robotframework.org/SeleniumLibrary/Selenium2Library.html
https://www.w3schools.com/xml/xpath_syntax.asp

Wait until element is visible, set focus and then get element by text :
Wait Until Element Is Visible //label[text()="Next Steps"] timeout=${time}s
Set Focus To Element //label[text()="Next Steps"]
Get Text //label[text()="Next Steps"]

Related

Interact with pseudo-elements with Selenium Webdriver?

I am working with Selenium Webdriver with Java.
And I was trying to interact with anchor tag which is enclosed as pseudo element ::before
But I am unable to interact with anchor element.
Here is the screenshot of the HTML structure.
With
JavaScriptExecutor, I understand, we can fetch the propertyValue using window.getComputedStyle().getPropertyValue() but I am not sure, how to interact with <a> element and execute a Click.
Initially, I attempted to click on the Anchor Element without considering the pseudo element as simple Element Interaction.
To fetch the Element:
private By tabRawView_By_CSS = By.cssSelector("[tabid='raw-view'][role='tab']");
But this piece is not throwing any error but it is also not clicking on the element.
Then I thought of using JavaScriptExecutor and was trying to run first in Developers Tool as below image but couldn't find suitable options.
Can anyone please suggest?
If it is Java bindings and all you want to do is to click on an achor tag which has Raw view as a text.
You could try with ExplicitWaits :
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Raw View"))).click();

Couldn't locate the search bar

I need to locate a search bar to search some text listed in the below box and click on it. I tried below code but I couldn't perform the activity.
This code wasn't clicked the search bar:-
driver.findElement(By.xpath("//input[#class='Searchbar__search-field___2FQ0S search-input']")
Image of the HTML:
The desired elements are ReactJS enabled elements within a Modal Dialog so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.modal-body#promotion-url-modal-body input.search-input[placeholder='Find a promotion...']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='modal-body' and #id='promotion-url-modal-body']//input[contains(#class, 'search-input') and #placeholder='Find a promotion...']"))).click();
Do you need to switch to a new frame before doing anything in the modal dialog? Do this:
driver.switchTo().activeElement();
And then try the following CSS locator:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("#promotional-url-modal-overlay input[placeholder='Find a promotion...']"))).click();
Can you provide us with a link to the website you are trying to interact with? Chances are the element is within an iframe, in which case you need to switch to the iframe before you can interact with said element.
Make sure your window is maximised, as this can interfere with iframes:
driver.manage().window().maximize();
Then you will need to find the id of the iframe, using something like firebug
Once you have the id:
driver.switchTo().frame("xxxxxxxxx");
interact with said element within the iframe:
driver.findElement(By.xpath("html/body/a/img")).click();
class can be changed, you need to check HTML on failed step. As a workaround you can use more generic xpath:
driver.findElement(By.xpath("//input[contains(#class, 'search-input')]"))

Robot Framework - Locating input element with accept attribute fails

I am writing an automation script for an avatar upload module with the following CSS locator:
input[accept="image/png,image/jpeg,image/gif,image/bmp"]
I am using Robot Framework's Wait Until Element Is Visible keyword to look for the locator above but is unsuccessful with the error:
Element 'css=input[accept="image/png,image/jpeg,image/gif,image/bmp"]' not visible after 30 seconds.
Increasing the timeout also doesn't work. Using the same in Chrome Dev Tools would successfully find the element. My guess is that the commas/slashes are messing with Robot's locator parsing. My question is: What is the correct way to write the locator?
Though present in the DOM, an element may not be visible/rendered. This is very often the case with file upload input elements - the UI renders something different, a button, div that had applied styling and fits in better with the overall design.
Thus a check is it visible will rightfully fail. Change your pre-usage approach to validate the input is in the HTML - this is actually the same as what you did in the browser's dev tools - with the Page Should Contain Element keyword, and proceed on success.
There is no problem with the CSS locator your are using. Maybe the element is in another iframe?

WebElement is identified through xpath in the developer console but the element wont be identified through selenium findElement method

The html element Im trying to locate is the "Shared" link.
I wrote a dynamic xpath to locate the element and it shows as the element was identified in the developer console.
But when i use the xpath that i wrote in the developer console to locate the element using selenium, it does not locate the element.
The method i used to check if it locates the element is shown below.
I could not figure out why this issue occurs, Is it because of a issue in the xpath that i have written or because of another issue?
Code you can try out is :
new WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.xpath(" your Xpath ")));
driver.findElement(By.xpath("your Xpath")).click();
The Xpath you have written would work if only one title is present on current page.
driver.find_elements doesn't have attribute to click() so use driver.findElement instead of Elements

selenium invalid element state: Element is not currently interactable and may not be manipulated

I have this issue with selenium; I cannot find i textinput : it always raises this exeception:
Element is not currently interactable and may not be manipulated
in this line:
Driver.FindElement(By.Id("ctl00")).Clear();
I try to put waiting like this:
Waiting.Until(driver =>(By.Id("ctl00")));
and
Waiting.Until(ExpectedConditions.ElementExists(By.Id("ctl00")));
but no luck.
If the exception gets thrown on Clear(),
the element is most likely present on the page and the input in a readonly state.
Try to validate XPath first as might be that targeting more than one element.
Due to this, element not currently interactable and may not be manipulated.
I faced same issue and found solution with above as my XPath was locating multiple element which exists on DOM but not page.
Correct the element XPath, error will resolve automatically with adding webdriver wait.
I hit the same error, was also a bit confused, but resolved it using the below wait condition. The elementToBeClickable condition checks that the element is both visible and enabled. Perhaps visibility is enough, but Selenium throws an explicit ElementNotVisibleException, so I'm not sure why I wouldn't get that...
Anyways, I opted to use a more robust wait condition, and it has worked well for me.
wait.until(ExpectedConditions.elementToBeClickable(By.id("myElementId"))).clear();
This happens sometimes when element is not visible. It would happen even when element is visible also.
Try using with different tags like xpath or etc.
You can check the element status using below script
driver.findElement(By.className("")).isEnabled()
It is due to element not visible so apply more wait while performing the action or you can use hardcoded sleep cause sometimes webdriver's wait doesn't work.
You are trying to perform .clear() on a WebElement, which is not an input tag. For example, if you try to do clear on a Span WebElement, You will get Selenium Invalid Element State error.
Waiting.Until(ExpectedConditions.ElementExists(By.Id("ctl00"))); should work.
Set break point at Waiting.Until() line and input $$("#ctl00") in Chrome console to check if the element is existing.
Make the driver to wait until an element is visible.
WebDriverWait wait = new WebDriverWait(driver, timeout);
wait.until(ExpectedConditions.visibilityOf(element));
Hope this helps!
If waiting for visibilityOf(), elementToBeClickable(), element.isEnabled() doesn't do the trick, try to look at the attributes of your element.
It may have some attributes responsible for read only state, in my case this was aria-readonly and aria-disabled, Hence I created custom wait like this:
webDriverWait.until(webDriver -> webElement.getAttribute("aria-readonly").equals("false") && webElement.getAttribute("aria-disabled").equals("false"));
Use this you will not get this error(might be scroll is not working here so use keypad TAB to move on, my problem got resolved by this)
Actions act = new Actions(driver);
act.sendKeys(Keys.TAB).build().perform();
act.sendKeys(Keys.RETURN).build().perform();
You might be asking an element with the same attribute like duplicate id, name etc.
I My case i was entering password in confirm password TextBox whose Id was "confirmPassword" but the same Id is used for the "Confirm Password" label for that TextBox.
So selenium was trying to perform sendKeys on the label instead of TextBox so getting "InvalidElementStateException", After that i changed the xpath and it worked.
So when you get "InvalidElementStateException" you should first check whether there are any duplicate element available with the xpath you have generated, and that not-interactive element could be a Button, Label or disabled element that causes the exception occurred.
I was clicking on a div element or label to element to clear the text in input tag. Changed my xpath to input tag and then I did not received any exception and the test was clear.