How to get the property value of a CSS class using selnium RC
I tried it using webdriver but can't get what is required
You can use the getEval(String script) command to evaluate javascript to fetch the property.
Selenium can be pretty limited in this sense
EDIT:
this.style.someAttribute will give you the value of someAttribute css style for the given Selenium node.
Also, if you want to run JavaScript on elements within the document body, such as document.getElementById .., you need to preceed your JavaScript string with "this.browserbot.getCurrentWindow()". For example, if I wanted to get the innerHTML of the element with id 'page_title', I would call
String title = getEval("this.browserbot.getCurrentWindow().document.getElementById('page_title').innerHTML");
This will evaluate the JavaScript in the context of the window you are testing. Failure to include this will execute JavaScript within the context of the Selenium frame. See this documentation on the Selenium API.
Related
I learn Selenium in Java and I'm struggling with little problem.
I'm working on handling dropdowns and to resolve my probelm I have to use Select class.
I wrote a selector:
#FindBy(css="#speed")
WebElement selectSpeed;
Then I wrote a method :
public SelectMenuPage selectRandomSpeed(){
getWaitForTheElement().untilElementIsPresentCss("#speed");
//webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(locator)));
Select select = new Select(selectSpeed);
select.selectByIndex(0);
return this;
}
The problem is that when I use Select class the code simply does not work and I receive:
org.openqa.selenium.ElementNotInteractableException: element not interactable:
Element is not currently visible and may not be manipulated
It works very well when, instead of using Select, I just put the selectors of all the wanted elements and just simply interact with them. Unfortunately, I have to use Select class.
Here is my DOM
As per the HTML, the html-select tag is having the style attribute set as display: none;.
Unless the element is visible within the HTML DOM Select() may not work properly.
I am creating automation testscipt for a webapps using angular to develop frontend. There is a form with multiple input field inside a fieldset tag. I use Chrome DevTools to find xpath of the above input field but when run the script with my framework, webdriver cannot find the element with my xpath. Webdriver can click on the fieldset tag but cannot interact with any element inside the fieldset tag. I try debugging and put breakpoint at the step which input text to the input field but it doesn’t work. Please let me know what’s happen with the element inside the fieldset tag! Thanks a lot!
Consider the following <select> from an internal Selenium test page:
<select id="invisi_select" style="opacity:0;">
<option selected value="apples">Apples</option>
<option value="oranges">Oranges</option>
</select>
It is used to simulate an invisible element as the id suggests, which is done by setting opacity to 0.
Although the element is not visible, a user can actually interact with it. If I open the page in a browser and click on the element's position, the select menu opens. I believe this is also why WebElement#isDisplayed() returns true for this element, which is also what these old Selenium issues suggest:
https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/1610
https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/1941
To execute actions such as clicks, we recently switched to the Java interactions API for several reasons, e.g., to prevent ElementClickInterceptedExceptions. (Please note that this is not about refactoring a bunch of Selenium tests, this happens in the context of a generic action executor that operates on top of the Selenium API.) However, if I do something like:
WebElement applesOption = /* get apples option */
new Actions(webDriver).moveToElement(applesOption)
.click()
.perform();
Moving to the element throws the following exception:
org.openqa.selenium.JavascriptException: javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite.
I guess this is because elementsFromPoint() via the WebDriver Actions API seems to return a "non-finite" double for transparent elements like this?
Is there a way to prevent this from happening when using Actions? Maybe, in addition to checking if the element is clickable (ExpectedConditions#elementToBeClickable(...)), I would have to parse—which sounds horrible—attributes such as opacity?
I just tried your sample file locally and the code below is working with no exceptions.
WebElement e = driver.findElement(By.id("invisi_select"));
Select select = new Select(e);
select.selectByValue("apples");
System.out.println(select.getFirstSelectedOption().getText());
select.selectByValue("oranges");
System.out.println(select.getFirstSelectedOption().getText());
It prints
Apples
Oranges
This error message...
org.openqa.selenium.JavascriptException: javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite.
...implies that the WebDriver instance was unable to focus on the element for one or other reasons:
The element havn't loaded properly when you tried to interact with it.
The element haven't got the focus.
Solution
Instead of using the Java interactions API you can use the Select Class and you can use either of the following Locator Strategies:
Using cssSelector and selectByValue():
Select s = new Select(driver.findElement(By.cssSelector("select#invisi_select")));
s.selectByValue("apples");
Using xpath and selectByVisibleText():
Select s = new Select(driver.findElement(By.xpath("//select[#id='invisi_select']")));
s.selectByVisibleText("Apples");
References
You can find a couple of relevant detailed discussions in:
javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite
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");
HTML Code
<label for="ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_21">Royality Free</label>
Selenium Code
driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_21")).getText();
The above selenium code is not working even i tried getAttribute();
its showing NullPointerException
You are trying to read text from the label but you are finding an element which has id ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_21 This is not the id of the label.
Your code should be:
WebElement labelElement = driver.findElement(By.cssSelector("label[for="ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_21"]"));
System.out.println(labelElement.getText());
This should work.
Moreover, the locator: ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_21 seems to be a randomly generated locator. Just confirm that it's not such a case. If it is then you will need to change your locating strategy.