Webdriver visibility of element without locator - selenium-webdriver

I want to check the text of my string(client) inside tag using the following code:
boolean feedBack = driver.findElement(By.cssSelector("body")).getText().contains(client);
If it returns true then, Is there any way to directly check the visibility of this string (without locator)?

Find an element that contains the needed text in its text node:
WebElement element = driver.findElement(By.xpath("//*[text()='" + client + "']"));
If needed, use normalize-space() or contains() instead:
WebElement element = driver.findElement(By.xpath("//*[contains(text(),'" + client + "')]"));
This will find the innermost element containing the text, not its any random ancestor.
Check whether it's visible via
element.isDisplayed()
Note that you have to be sure your text only appears once on the page for this to be okay. But the solution can be easily adapted for more elements, too.

Related

Returning a value of a span tag using selenium.webdriver.find_element_by_xpath()

I am trying to extract the current video time from a youtube video as it is playing.
I've inspected the youtube page to find the element where the current time value is stored. This is the element below.
span class="ytp-time-current">0:03</span>
I had to remove the strict inequality signs before and after span because it returns 0:03 and the entire tag can't be seen.
I've tried searching finding the value using wd.find_element_by_class_name("ytp-time-current") but returns none
I've also copied the xpath for the element:
//*[#id="movie_player"]/div[20]/div[2]/div[1]/div/span[1]
and tried finding the value using the _xpath( )
code I've tried
from selenium import webdriver
wd = webdriver.Chrome(executable_path = r'pathtodriver')
wd.get('https://www.youtube.com/watch?v=N__fRbG84fI')
search = wd.find_element_by_class_name('ytp-time-current')
value = wd.find_element_by_xpath('//*[#id="movie_player"]/div[22]/div[2]/div[1]/div/span[1]').get_attribute('Value')
I expected to get an integer value or at least a string. but both scenarios return none.
A <span> tag doesn't have a value attribute. You probably want to do:
value = wd.find_element_by_xpath('//*[#id="movie_player"]/div[22]/div[2]/div[1]/div/span[1]').text
(I'm assuming this is the Python bindings)

How to Print the Disabled text from Geb/Groovy?

enter image description here
We are automating the UI Application, Our UI application have Disabled Text are present, so we need to Validate the Disabled text. Before validating, I have to Print the Disabled text, Please guide me to how to print the text using Geb/Groovy.
Please find the Image of HTML tag which i highlighted is the Disabled text
BNSF0000712570
BNSF0000712570
The selector above will yield multiple results, i.e. elements, if there is more than one element that matches the classes used in the By.cssSelector query.
To get only the element containing "BNSF0000712570", I would suggest you try to get it using the "ext:qtip" attribute instead (which I assume is unique per element containing a disabled text) on the div containing the disabled text:
def myText = $(“div[ext:qtip=‘Id: 0001’]”).text();
println myText;
assert myText == "BNSF0000712570";
#Saurabh Gar: Why would you use the WebDriver "By" class selectors? With Geb you have access to a wide range of simpler ways to write selectors, e.g. like the one used above.
You should try using By.cssSelector as below :-
def text = driver.findElement(By.cssSelector("td.x-grid3-td-elementvalue").text
Or
def text = driver.findElement(By.cssSelector("div.x-grid3-col-elementvalue").text
assert text == "BNSF0000712570"
println text
Note:- If still doesn't get the text need to share table HTML insteadof screenshot that's why, could make a best locator.
Hope it helps..:)

How to search an element by its name in a web page using selenium webdriver

I am creating groups randomly. Then i need to check whether that group has been created or not. is there any way to search the group by its given name.
or element in a webpage by its name. i am trying it by using By.name locator, but not able to do that.
I just want to get an element in webpage by its name which is given by me/user.
For Ex: i have created a group "gr907", So how i can search or verify whether group having name "gr907" has been created or not.
Kindly Suggest
Given the example element you gave in the comment for your initial post - <span class="grpName">Gr9006</span> - and assuming that other relevant elements share the same class attribute, you could use the following example to retrieve a list of suitable WebElements:
List<WebElement> elements = driver.findElements(By.className("grpName"));
If this doesn't work, then your next course of action could be to make an XPath expression that identifies span tags whose inner text starts with "Gr", such as:
List<WebElement> elements = driver.findElements(By.xpath("//span[contains(text(),'gr')]"));
To find a single element containing the group name created at run time as its exact text content, you can simply pass it in to an XPath like so;
WebElement element = driver.findElement(By.xpath("//span[.='" + group name + "']");

Codename One - get selected text from AutoComplete

How can I get the complete selected text from an AutoComplete TextField?
If I use getText(), I only get the few letters the user has input so far.
Example: I write "flo" and then select "Flowers" from the list, but getText() gives me "flo"
AutoCompleteTextField auto = new AutoCompleteTextField(arrayWithNames);
auto.setMinimumLength(4);
auto.addListListener((ActionEvent evt1) -> {
String lookedFor = auto.getText();
Hashtable<String,Object> match[] = findMatch(lookedFor);
if(hMatch.length>0){
contElements.removeAll();
for (Hashtable<String, Object> Match1 : match) {
...
...//fill the Container with the names found
...
}
}
});
How it works
I am using the AutoComplete TF as a search button.
I have an array with all the names in my list.
Then I populate the Auto with the array.
The user selects a name from the Auto and then I search the value that is being "lookedFor" using the findMatch(). It returns a new array with the found entries.
I need the complete name from the list so I can use the findMatch() method, but when I use getText() from the Auto, it only returns the letters the user entered, and not the whole name, so my method does not work, since I am comparing whole Strings.
(I am using the Auto because it is very convenient if people remember only a part of the name they are looking for)
If you subclass AutoCompleteTextField you can access the selected text internally via getSuggestionModel().getItemAt(getSuggestionModel().getSelectedIndex()). Now you can define a public getter method getSelectedText() or something on your derived class.
I am not sure you are using the AutoCompleteTextBox correctly.
The entire purpose of the AutoCompleteText box is to help you assist the user in selecting from a list of valid requests,
You should not be getting the value of getText() until the user is ready to submit the form where the AutoCompleteTB is located.
This WILL help if you haven't already looked here:
https://www.codenameone.com/javadoc/com/codename1/ui/AutoCompleteTextField.html#getPropertyTypes--
Good luck!

Selenium WebElement value empty after sending keys

I'm running some simple form tests where values are added fields.
After each value is added to a field:
input.SendKeys(value);
I want to check the value in the field is correct. This may sound unusual but the field may have an ajax search attached and if the search doesn't pull back a match, the field will be empty.
I've tried testing the text value of the WebElement after sending the keys but it always seems to be empty:
bool match = input.Text.Equals(value);
// input.Text always seems to be an empty string
I'm using Selenium 2 with the WebDriver - is there another way to perform these checks? Is there a particular reason why the WebElement is empty even if the SendKeys successfully prints a value (actually in the browser) in to the WebElement (textbox)?
Any help would be grateful.
It may be possible that the text value that you are entering is assigned as a "value" attribute of text box and not as "text"
input.sendKeys(enteredValue)
String retrievedText = input.getAttribute("value");
if(retrievedText.equals(enteredValue)){
//do stuff
}

Resources