How to get the all element id's of a screen in selenium - selenium

How can I get the all element id's of a screen in selenium?
Please refer to this screenshot
Element ID is getting changed every time the page loads. I used contains#id , start-with#id, but it doesn't work every time. Now I want to get all the element id's from the webpage, so I can select the exact element.
My webpage contains input text, buttons, drop-downs.

Use Xpath instead or cssSelector. If you persist on knowing all page ids as a list of String in java, try to execute a javascript function.
// The Firefox driver supports javascript
WebDriver driver = new FirefoxDriver();
// Go to the Google Suggest home page
driver.get("http://www.google.com/webhp?complete=1&hl=en");
ArrayList ids = (ArrayList)((JavascriptExecutor) driver).executeScript(
" var allElements = document.getElementsByTagName('*'); var allIds = []; "
+ " for (var i = 0, n = allElements.length; i < n; ++i) { "
+ " var el = allElements[i]; "
+ " if (el.id) { allIds.push(el.id); } } return allIds; "
);
Regards,
Alan Mehio
London, UK

Related

How to read canvas element attribute value using selenium?

I want to read canvas element value ("Answer:5015") but attribute value does not exist. Can you please let me know how to read attribute value ?
App URL : https://the-internet.herokuapp.com/challenging_dom#edit
This code works, using Java and Edge browser:
System.setProperty("webdriver.edge.driver", "msedgedriver.exe");
WebDriver driver = new EdgeDriver();
driver.get("https://the-internet.herokuapp.com/challenging_dom#edit");
String answer = new String();
ArrayList<WebElement>scripts = new ArrayList<WebElement>((ArrayList<WebElement>) driver.findElements(By.tagName("script")));
for(int i = 0; i < scripts.size(); i++) {
String focusText = scripts.get(i).getAttribute("innerHTML");
if(focusText.contains("canvas.strokeText")) {
answer = focusText.substring(focusText.indexOf("Answer"), focusText.indexOf("',"));
break;
}
}
System.out.println(answer);

Appium - not able to get the child element

preconditions:
PL: Java
java client 7.0.0
Appium server version: 1.13.0
Device Samasung Galaxy S
Android Version 9.0
Hello All,
I am trying since a day to get the error text field in email field in screen shot below, but I am not able to get this.
I have already tried the following :
1. actual_textInputError = driver.findElementByXPath("//android.widget.LinearLayout//android.widget.RelativeLayout//android.widget.RelativeLayout//android.widget.LinearLayout//android.widget.FrameLayout//android.widget.LinearLayout//android.widget.FrameLayout//android.widget.TextView").getText();
2. List<MobileElement> elements = driver.findElements(By.id("textinput_error"));
for (MobileElement element : elements) {
actual_textInputError = element.getText();
3. actual_textInputError = email_Field.findElementByClassName("android.widget.TextView").getText();
// List <MobileElement> rel_Layouts = driver.findElementsByXPath("*//android.widget.RelativeLayout");
4. MobileElement layout = (MobileElement)driver.findElementByXPath("//android.widget.RelativeLayout[contains(#resource-id, 'emailInput')]");
List <MobileElement> errors = driver.findElementsById("textinput_error");
actual_textInputError = errors.get(0).getText();
actual_textInputError = errors.get(1).getText();
actual_textInputError = errors.get(2).getText();
// MobileElement email_error_field = layout.findElementByXPath("//android.widget.RelativeLayout[contains(#resource-id, 'textinput_error'");
// actual_textInputError = email_error_field.getText();
/* for (int i = 0; i<errors.size();i++ ) {
actual_textInputError = errors.get(0).getText();
}*/
/* for (MobileElement rel_Layout : rel_Layouts) {
actual_textInputError = element.getText();
System.out.println("error text = " + actual_textInputError);
}*/
// actual_textInputError = driver.findElementsByXPath("//android.widget.RelativeLayout//android.widget.TextView").get(0);
// errorMessage_Text
//actual_textInputError = errorMessage_Text.getText();
is possible that some one send me the Xpath to search.
many Thanks in advance
Kindly use the below XPath.
//android.widget.TextView[#text='E-mail address is already in use']

How to write locator for text between div and span (Preferably xpath) which contains non-breaking space (&nbsp)

I want to write xpath for the following div:
<div class='someclass' id='someid'>:TEST SELENIUM 1234<div>
Please note :
tag can be anything such as div, span ,or anchor tag.
can be present anywhere in the text.
What I have tried so far :
//div[contains(text(),":TEST SELENIUM 1234")]
//div[contains(text(),":TEST{ }SELENIUM{ }1234")]
//div[contains(text(),":TEST SELENIUM 1234")]
//div[normalize-space(text()) = ':TEST SELENIUM 1234']
//div[normalize-space(text()) = ':TEST{ }SELENIUM{ }1234']
//div[normalize-space(text()) = ':TEST SELENIUM 1234']
//div[normalize-space(.) = ':TEST SELENIUM 1234']
//div[normalize-space(.) = ':TEST{ }SELENIUM{ }1234']
//div[normalize-space(.) = ':TEST SELENIUM 1234']
//div[normalize-space(.) = ':TEST{\u00a0}SELENIUM{\u00a0}1234']
//div[normalize-space(.) = ':TEST${nbsp}SELENIUM${nbsp}1234']
What has worked for me (thanks to #Andersson)
//div[starts-with(text(), ":TEST") and substring(text(), 7)="SELENIUM" and substring(text(), 16)="1234"]
This is more of a work around and would work only for known Strings.
These are the SO post which I have already followed :
Link1
Link2
Any help will be highly appreciated.
According to the conversation above, you can use this sample xPath builder(JAVA):
public class Test {
public static void main(String[] args) {
String s = ":TEST SELENIUM 1234";
String[] parts = s.split(" ");
StringBuilder xpath = new StringBuilder("//*");
for (int i = 0; i < parts.length; i++){
xpath.append((i == 0) ? "[contains(text(), '" + parts[i] + "')" : " and contains(text(), '" + parts[i] + "')");
}
xpath.append("]");
System.out.println(xpath);
}
}
Output:
//*[contains(text(), ':TEST') and contains(text(), 'SELENIUM') and contains(text(), '1234')]
In Python I would do
required_div = [div for div in driver.find_elements_by_xpath('//div') if div.text == ':TEST SELENIUM 1234'][0]
to find required node by its complete text content ignoring non-breaking space chars
P.S. Again it's just a workaround, but it seem to be quite simple solution

Java - Want to produce a list of returned google search url's and then match them to a different url to find a hit - Selenium

I'm trying to create a program that will search keywords in google and then match the search results with a website url and if the url matches it will save the page and position on page in which that url was found. However, whenever I print the size of the list it returns 0
code:
driver.get("http://www.google.com");
element = driver.findElement(By.id("lst-ib"));
element.sendKeys(keyword);
element.sendKeys(Keys.RETURN);
int page = 0;
HashMap<Integer, Integer> hitList = new HashMap<Integer, Integer>();
for(int i=page;i<=pageNo;i++) {
List<WebElement> list = driver.findElements(By.className("_Rm"));
System.out.println(list.size());
Thread.sleep(10000);
for(int j=0;j<list.size();j++) {
String site = list.get(j).getText();
if(site.contains(website)) {
hitList.put(i, j);
}
driver.findElement(By.xpath(".//*[#id='pnnext']/span[2]")).click();
Thread.sleep(10000);
}

How to handle Mouseover in Selenium 2 API

String strPrimaryNav = "MEN";
String strSecondaryNav = "Shoes";
String strTertiaryNav = "Golf";
driver.findElement(By.linkText(strPrimaryNav)).click();
WebElement weSecNav = driver.findElement(By.className("secondaryButton").linkText(strSecondaryNav));
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseDown(((Locatable)weSecNav).getCoordinates());
//just trying with for loop as the tertiary popup disappears quickly and hence getting ElementNotVisible Exception
for (int i = 0 ; i< 2; i++){
mouse.mouseDown(((Locatable)weSecNav).getCoordinates());
//mouse.mouseMove(((Locatable)weSecNav).getCoordinates(), 0, 0 );
//WebElement weTerNav = driver.findElement(By.className("tertiaryButton").linkText(strTertiaryNav));
WebElement weTerNav = driver.findElement(By.linkText(strTertiaryNav));
boolean isSecDisplayed = ((RenderedWebElement)weTerNav).isDisplayed();
System.out.println("isDisplayed: " + isSecDisplayed);
System.out.println(" " + ((RenderedWebElement)weSecNav).getAttribute("href"));
System.out.println(" " + ((RenderedWebElement)weSecNav).getValueOfCssProperty("action"));
weTerNav.click();
}
I was trying the below code using selenium 2 but, the tertiary popup not stays long to click it and hence getting ElementNotVisible exception at Tertiary click.
You can at least check that the element is visible before you send your click:
Selenium s = new WebDriverBackedSelenium( driver, URL );
s.waitForCondition( "!( document.getElementById('.....') === null )", "20000" );
s.click( .... );
This avoids the exception. I'm not sure there is a way to make the popup stay any longer than it should.