i'm trying to automate a app to do everything that you would have to do by hand. My main goal right now is to get it to type my email in the "Enter your email address" field.
But I've ran into a bump while trying to find the resource-id element in uiautomatorviewer.bat
There is no text in the resource-id field.
Is there anyway to get around this? How else could I find the element to plug into my
driver.findElement(By.id("com.offerup:id/")).sendKeys("xxxxxx#gmail.com");
Use Xpath for this
By path = By.Xpath("//*[#text='Enter your email address']");
driver.findElement(path).sendKeys("xxxxxx#gmail.com");
You xpath aa follows
//android.widget.EditText[#text='Enter your email address']
And use sendKeys() for what ever you need to use.
You can also try like this to find text based on classname and then perform sendKeys
List<WebElement> offerElements=
driver.findElements(By.className("android.widget.EditText"));
for (int i=0;i<offerElements.size();i++) {
if (offerElements.get(i).getText().equals("Enter your email address"))
offerElements.get(i).sendKeys("email text");
}
Related
I'm trying to get the value of a permission key in Selenium web-driver(java), but I couldn't able to identify the element. Can anyone please help me to identify the element and get the value "4BF12-50763-166E0".
There are 9 span class with same name and it's quite hard to identify the same.
The web page part look like as below and i want to take each of the below elements value like version, status etc. All are present in the span class as shown in the below pic.
Any suggestions will be of great help.
Version 1.2
Status Enabled
Days left 373
Permission KEY 4BF12-50763-166E0
Permission Serial Number 99678905096711
Subscription End Date 2020-08-31
The sample of the HTML is attached.
I have tried this, but it is selecting "Permission KEY" only.
WebElement details = driver.findElement(By.xpath("//b[text()='Permission KEY']"));
String title = details.getAttribute("title");
String label = details.getText();
System.out.println("Title is " +title);
System.out.println("Label is" +label);
You can find your desired text in this case "Permission Key" go the ancestor element and go to the second span that contains the value.
XPath:
String xpath = "//b[text()='Permission Key']/ancestor::dd/span[2]";
WebElement spanElement = driver.findElement(By.xpath(xpath));
spanElement.getText(); // Will give you '4BF12-50763-166E0'
IMPORTANT NOTE: You can change the 'Permission Key' to any other value like for example 'Version' and IF they follow the same pattern, it will work for all, example:
String xpath = "//b[text()='Version']/ancestor::dd/span[2]";
WebElement spanElement = driver.findElement(By.xpath(xpath));
spanElement.getText(); // Will give you '1.2'
You can use preceding-sibling strategy in your xpath, try this bellow :
WebElement key = driver.findElement(By.xpath("//span[#class='pull-right property-value' and ./preceding-sibling::span/b[contains(text(),'Permission KEY')]]"));
System.out.println("Permission KEY : " +key.getText());
Make sure this is the correct class name : #class='pull-right property-value', because i see from your image.
I'm not able to get an xpath using FirePath. I want to locate username field of a login form using xpath in Firepath. But when I try to find the same element,
I'm getting xpath like :
html/body/div[3]/div/div/div/div/div[2]/div/form/div[1]/input
How can I use it in Selenium script ?
You can use it like:-
WebElement ele1 = driver.findElement(By.xpath("html/body/div[3]/div/div/div/div/div[2]/div/form/div[1]/input"));
ele1.sendKeys("username");
Here username in double quotes would be replaced by actual value you want to enter in username field.
This is also xpath which is called absolute xpath If you are looking for relative xpath then uncheck the option as shown in below image
And you can use this xpath like :
driver.findElement(By.xpath("html/body/div[3]/div/div/div/div/div[2]/div/form/div[1]/input")).sendKeys("username");
You can do this via the by.xpath
webdriver.findElement(By.xpath("html/body/div[3]/div/div/div/div/div[2]/div/form/div[1]/input")).click();
You can also search for a relative xpath witch is more robust have a look at this for more information of xpath.
Also have a look at locators in selenium for more information about the diffrent locators
I found a few answers about this but they did not answer my question an so I am writing a new question.
I have HTML code having below kind of checkbox elements (in browser's inspect element)
<input role="checkbox" type="checkbox" id="jqg_TransactionFormModel501EditCollection2_147354_grid_-1274" class="cbox" name="jqg_TransactionFormModel501EditCollection2_147354_grid_-1274" value="true">
In my test case I want to click on checkbox using its ID using Selenium Webdriver.
here Id= "jqg_TransactionFormModel501EditCollection2_147354grid-1274" is dynamic.
in above id, Bold & Italic marked letters (dynamic) will change with different check boxes in same page as well as page refresh.
Bold marked letters (dynamic) will change on page refresh only (remain same through all the check boxes in same page.)
How shoud I format/write XPATH so that I can click on desired check boxes using below statement.
WebElement checkbox = webDriver.findElement(By.id("idOfTheElement"));
if (!checkbox.isSelected()) {
checkbox.click();
}
Thanks for your help in advance.. !
Here are a few examples of xpaths which you can use to find your checkbox
//input[contains(#id,'jqg_TransactionFormModel')]
OR, if you want more checks, try something like
//*[starts-with(#id,'jqg_TransactionFormModel') and contains(#id,'EditCollection2_')]
Additionally, you can try regex as well using matches
//*[matches(#id,'<regex matching your id>')]
You can use partial ID using cssSelector or xpath
webDriver.findElement(By.cssSelector("[id*='TransactionFormModel']"));
webDriver.findElement(By.xpath("//input[contains(#id, 'TransactionFormModel')]"));
You can replace TransactionFormModel with any other fixed part of the ID.
As a side note, no need to locate the element twice. You can do
WebElement checkbox = webDriver.findElement(By.id("idOfTheElement"));
if (!checkbox.isSelected()) {
checkbox.click();
}
You can write xpath like this :
//input[starts-with(#id,'jqg_TransactionFormModel')]
//input[contains(#id,'jqg_TransactionFormModel')]
I recommend not using ID's or xpath and adding a data attribute to your elements. This way, you can avoid the annoyance of xpath and have a strong selector that you feel confident will always be selectable.
For example, you could call the attribute: data-selector. You can assign a value of "transactionFormModelCheckbox". Then, when creating a new element, you create by css selector with the value referenced above.
No matter what type of formatting, you'll always be able to select that checkbox - as long as the element exists in the DOM. If you need to investigate other attributes, you can use the selector to do things like hasClass() or anything else you need.
Hope that helps !
Trying to find an xpath expression to use in:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("XPATH HERE"))).click();
The element says "Invite Users" on the page and i need to be able to click on it
I need to find the element /a[#id='inviteUsers_8ef17ba4-b739-4fb6-8198-3862ea84c381_toggle'] but the problem is the characters after "inviteUsers_" is dynamically generated
I have already tried these:
"//*[contains(.,'Invite Users')]";
"//a[contains(.,'Invite Users')]";
And these give NoSuchElement exceptions.
This is the complete XPATH:
/html/body/div[#class='col-xs-10 col-xs-offset-2 main']/fieldset[#class='form-horizontal']/div[#id='roles']/div[#id='entitlements']/div[#class='panel panel-default '][3]/div[#id='service_8ef17ba4-b739-4fb6-8198-3862ea84c381']/div[#class='panel-body']/div[#class='panel panel-default'][1]/div[#class='panel-heading']/h4[#class='panel-title']/a[#id='inviteUsers_8ef17ba4-b739-4fb6-8198-3862ea84c381_toggle']
You can solve it with starts-with():
//a[starts-with(#id, "inviteUsers_")]
If this does not work try find a unique parent.
//div[contains(#id, 'service')]//a[contains(#id, 'inviteUsers')]
I have a login button and want to identify it using xpath and id. But both the xpath and id are dynamically changing. Please advise on how to do it.
The Xpath and ID keep changing as below for login button and i am not able to find any common element to find it:
Xpath : .//*[#id='uO4Qo']
ID : uO4Qo
Xpath : .//*[#id='tP5Qo']
ID : tP5Qo
Xpath : .//*[#id='vVBPn']
ID : vVBPn
If you're using Selenium IDE to create your script, the target field is a dropdown, displaying all the possible options to identify your specified button. If the options CSS, name or anything else are not available, it means that these things are not present on your page. Perhaps is xpath:position an option in your specific case.
Use below code, use login button text in place of LoginButtonText:
driver.findElement(By.LinkText("LoginButtonText")).click();