asking for Selenium *find_element_by_class_name - selenium

I have tried to get the text from the helpful and unhelpful button but I couldn't succeed:
for example: I want to get "6013" for helpful points and "320" for unhelpful points:
my code is:
product_helpful.append(container.find_element_by_class_name('css-0').text)
product_not_helpful.append(container.find_element_by_class_name('css-0').text)
you can check the picture for your reference:
enter image description here

Use the title attribute to target the helpful vs unhelpful portion of the code
product_helpful.append(container.find_element_by_css_selector('[title="Helpful"] span').text)
product_not_helpful.append(container.find_element_by_css_selector('[title="Unhelpful"] span').text)
Note that for this you must use the find_element_by_css_selector method from the WebElement.

Related

Finding color of the text in selenium

Below is the code, trying to find the colour of the text 'Special Assistance' in spicejet.com. I am expecting rgb value as (196,18,48) (as shown in the developer view during inspection of the element)
however when i run the program it displays value as rgba (0,0,0,1);
driver.get("https://www.spicejet.com");
driver.manage().window().maximize();
WebElement ele = driver.findElement(By.xpath("//a[text()='Special Assistance']"));
System.out.println(ele.getCssValue("color"));
please help
That page looks like it has multiple elements that match that xpath. As such I think you're getting the wrong one returned and the color is actually correct.
If you're actually looking for the red special assistance link this might be a better (more specific) xpath:
//*[#id='discount-checkbox']//a[text()='Special Assistance']
Try that, and I think you may find the expected results.

How to find an element containing #nbsp; in text?

I've an element with html -
<h3>App-1 Playground Login</h3>
I want to identify it with entire text - App-1 Playground Login, but causing issues to identify it. Please help how this element can be identified.
Please use the below xpath. I have already tested that and it is working fine. In the second argument of the translate method you need to type "ALT+0160" and in the third argument you will have to put just a normal space.
//h3[contains(translate(text(),' ',' ' ), 'App-1 Playground Login')]
One of the way to select your title could be :
//h3[text()= concat('App-1 Playground',codepoints-to-string(160),'Login')]
Works fine on http://xpather.com/

I.click()-selector in CodeceptJS - how to find first button with specific innerHTML

I have various buttons and several buttons with the same name "Start". I need to click on the first found button with this name (innerHTML).
With jQuery this works with :
$('button:contains(Start):first').click()
How does it work with I.click()-Selector in CodeceptJS? I can't find the right syntax and always getting:
"invalid selector: An invalid or illegal selector was specified"
Here is the API for this function: https://github.com/Codeception/CodeceptJS/blob/master/docs/webapi/click.mustache
The only working solution I found is:
I.click('//button[1]');
But this solution is confusing, because you need to know the exactly number in the order of this element - and I have a lot of buttons with different names. Also this not allows me to search by innerHTML such as "Start".
You could use the I.executeScript like this:
I.executeScript("var elements = document.getElementsByName('Start');elements[0].click();"); or
I.executeScript("var elements =
document.querySelector(\"button[name*='Start']\");elements[0].click();");
You need using XPath for that
//button[1][contains(text(), 'Start')]
locate("//button[contains(text(), 'Start')]").first()
or
locate("//button[contains(text(), 'Start')]").at(1)
Works fine.

Selenium Command

When we use selenium command at that time command not find and attribute not get? See below command.
<table>
<tr><td>open</td><td>http://www.wikipedia.org/</td><td></td></tr>
<tr><td>verifyAttribute</td><td>css=input#searchInput</td><td>(Search Input)</td></tr>
<tr><td>assertAttribute</td><td>css=input#searchInput</td><td>(Search Input)</td></tr>
<tr><td>verifyAttribute</td><td>css=input#searchInput</td><td>language</td></tr>
<tr><td>verifyAttribute</td><td>xpath=//div[2]#class central-featured</td><td>central-featured</td></tr>
<tr><td>verifyAttribute</td><td>xpath=//div[2]#class central-featured</td><td>search1</td></tr>
<tr><td>assertAttribute</td><td>xpath=//div[2]#class central-featured</td><td>central-featured</td></tr>
</table>
I am using Selenium IDE 2.5.0 in Mozilla Firefox and Ubuntu.
Xpath //div[2]#class central-featured is invalid. Try changing it to //div[#class='central-featured']/#class if you mean to select a class.
You could also use assertElementPresent function instead of selecting attribute, if the whole point is to check that element exists, i.e.:
<tr><td>assertElementPresent</td><td>xpath=//div[#class='central-featured']</td><td></td></tr>
Much simpler that way.
Use xPaths in this case.
Use google chrome's built in developer tool for this
Place your cursor on the element
Press Ctrl+Shift+C
Click the Element
That clicked Element's code is highlighted in the short window on the bottom
Right-Click on highlighted code
Select Copy > Copy XPath
Here it is you have copied the xPath for that specific element. This is shown in the image:
Click to see how to copy xPath
The Xpath you have used in Invalid.
You can use xpath as follows and through this you can fins xpath of any object - just need to study the concept:
Here as we can see we want to search Google Search just by writing its xpath in console
So to find the Google Search button we have to write xpath like this
//span[#id='gbqfsa']
Once we hit enter it would bring
[ gbqfsa">​Google Search​​ ],
It shows that xpath for Google Search Button is correctly written
Now suppose we want to search Google Search button if we are just familiar that id attributes start with gbqfs
then we have to use function starts-with like this
//span[starts-with(#id,'gbqfs')]
and once when we hit enter on console it would reflect two button one is Google Searchand Second one is I’m Feeling Lucky
[
gbqfsa">​Google Search​​
,
​I'm Feeling Lucky​​
]
So to find out the Google Search uniquely we need to complete id attribute to gbqfsa
“//span[starts-with(#id,'gbqfsa')]
and hit to enter and now it would reflect only
[
​Google Search​​
],
It proves that we have written right xpath for Google Search
In the same fashion we can use Contains function to find the Google Search button like this
here I have taken fsa from gbqfsa
//span[contains(#id,'fsa')]
hit enter and hopefully it will return
[
​Google Search​​
],
if there are multiple attributes then we can use:
//span[contains(#id,'fsa') and contains(#class, 'xyz')] hit enter and hopefully it will return
Information Source: Sumit Mittal Blog
You can use CssSelector as below
webDriver.findElements(By.cssSelector("div.central-featured")) // for more than 1 elements with same class
webDriver.findElement(By.cssSelector("div.central-featured")) // for 1 element

Finding text on page with Selenium 2

How can I check whether a given text string is present on the current page using Selenium?
The code is this:
def elem = driver.findElement(By.xpath("//*[contains(.,'search_text')]"));
if (elem == null) println("The text is not found on the page!");
If your searching the whole page for some text , then providing an xpath or selector to find an element is not necessary. The following code might help..
Assert.assertEquals(driver.getPageSource().contains("text_to_search"), true);
For some reason, certain elements don't seem to respond to the "generic" search listed in the other answer. At least not in Selenium2library under Robot Framework which is where I needed this incantation to find the particular element:
xpath=//script[contains(#src, 'super-sekret-url.example.com')]
A simpler (but probably less efficient) alternative to XPaths is to just get all the visible text in the page body like so:
def pageText = browser.findElement(By.tagName("body")).getText();
Then if you're using JUnit or something, you can use an assertion to check that the string you are searching for is contained in it.
assertThat("Text not found on page", pageText, containsString(searchText));
Using an XPath is perhaps more efficient, but this way is simpler to understand for those unfamiliar with it. Also, an AssertionError generated by assertThat will include the text that does exist on the page, which may be desirable for debugging as anybody looking at the logs can clearly see what text is on the page if what we are looking for isn't.