selenium java. Unable to locate element - java

Please can someone point me to the right direction with below code?
driver.findElement(By.id("div#h4clock a.location").equals("London"));
I used getText("London") but it did not work.
I am quite new so any advise would be very much appreciated.
I also want to have a string to store the element London and display it using Println.
Many thanks in advance,
Hamid

The selector does not look like as an id that cssSelector. Try
driver.findElement(By.cssSelector("div#h4clock a.location")).getText().equals("London");
Edit:
WebElement city = driver.findElement(By.cssSelector("div#h4clock a.location"));
String getcity = city.getText();
System.out.println(getcity);

Related

What is best strategy to verify text of multiple elements without adding a ton of locators in a selenium framework?

I want to automate a test using selenium java in which I need to check whether a specific text is NOT present on the entire page.
The page has many elements where this text may be present. In general, if it were a few elements, I could resolve this via standard driver.findElement(By.xpath("//somepath")).getText(). But, I want to write an efficient test that doesn't have tons of locators just for this test.
You can use XPATH selector '//*[contains(text(), "YOUR_TEXT")] to find a text that you need.
Example of the code on Python:
def find_text_on_the_page(text):
selector = '//*[contains(text(), "{}")]'.format(text)
elements = browser.find_elements_by_xpath(selector)
assert len(elements), 'The text is not on the page'
Hope, this will help you.
You could try to locate it via xPath selector, but I would not recommend test like that.
Surely You know where to look for some test? At least a part of web page.
Here is code sample how to achieve this:
List<WebElement> list = driver.findElements(By.xpath("//*[contains(text(),'" + text + "')]"));
Assert.assertTrue("Text not found!", list.size() > 0);
or
String bodyText = driver.findElement(By.tagName("body")).getText();
Assert.assertTrue(bodyText.contains(text));
or in method:
public boolean isTextOnPagePresent(String text) {
WebElement body = driver.findElement(By.tagName("body"));
String bodyText = body.getText();
return bodyText.contains(text);
}
Hope this helps, but as I mentioned try defining a smaller scope for testing.

Get following sibling on android using Appium

driver.findElementByXPath("//android.widget.TextView[#text='a1492']/../following-sibling::android.view.ViewGroup").click();
This does not work. Am I missing anything?
#text is looking for an attribute text. If you're actually after the text content, it's [text()='a1492'].
You can try below, this will solve your problem:
driver.findElementByXPath("//android.view.ViewGroup[preceding-sibling::android.view.ViewGroup/*[#text='a1492']]").click();

java selenium - hidden input value

First Stack post, so don't be harsh if I get it wrong.
Im using selenium and java for some automated testing. All was going well until I tried to set the value of a hidden input.
When using 'type' in the Firefox IDE, it works a treat, but when I use the line below, it doesn't play at all. It just fails.
// This line doesnt like to run because its hidden
selenium.type("name=startDate_submit", "2015-09-25");
Can anyone point me in the right direction.
Many Thanks.
Edit:
WebDriver driver = new ChromeDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("$([name=startDate_submit]).attr('type', 'text');");
Thread.sleep(3000);
// This line doesnt like to run because its hidden
selenium.type("name=startDate_submit", "2015-09-25");
Should this be the way I do this? I just cannot get it working.
just try with this command,
driver.findElement(By.xpath("path")).sendKeys("value");
but make sure you have clicked that path before providing input value. come back if you still have any problem.
Try with available javascript commads like document.getElementById, then set the value.

Want to access attribute value

i need help regarding html parser i want to get the first attribute"href" value of tag "a" please resolve my problem.I want to fetch this link from the code http://myneta.info/gujarat2012/candidate.php?candidate_id=1591,
i am attaching the snap please see and provide some solution,
i have tried this code but not works for me -
String temp = source.getElementById("main").getFirstElementByClass("grid_9").getAttributeValue("a");
here is image
Please try the following:
source.getElementById("main").getFirstElementByClass("grid_9").getFirstElement("a").getAttributeValue("href")

Getting error Element not found in the cache - perhaps the page has changed since it was looked up, when i am trying to select an option from dropdown

I am getting error "Element not found in the cache - perhaps the page has changed since it was looked up" , when i am trying to select an option from dropdown. Please find my code below
WebElement searchID=driver.findElement(By.xpath("ctl00$ContentPlaceHolderView$ctlRegistration$DropDownList1"));
List <WebElement> searchOptions=searchID.findElements(By.tagName("option"));
for(int i=0;i<searchOptions.size();i++)
{
if(i==1)
{
String searchIdDropdown=searchOptions.get(i).getText().toString().trim();
System.out.println(searchIdDropdown);
driver.findElement(By.name("ctl00$ContentPlaceHolderView$ctlRegistration$DropDownList1")).click();
driver.findElement(By.name("ctl00$ContentPlaceHolderView$ctlRegistration$DropDownList1")).sendKeys(searchIdDropdown);
driver.findElement(By.name("ctl00$ContentPlaceHolderView$ctlRegistration$DropDownList1")).submit();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.findElement(By.name("ctl00$ContentPlaceHolderView$ctlRegistration$TextBox1']")).sendKeys(Keys.TAB);
driver.findElement(By.name("ctl00$ContentPlaceHolderView$ctlRegistration$TextBox1")).sendKeys(this.getProperties(Country+"_"+UserType+"_Registration_User1"));
driver.findElement(By.name("ctl00$ContentPlaceHolderView$ctlRegistration$TextBox1")).click();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
Please let me know how this error can be resolved.
Thanks in Advance,
Manasa.
So a few things to note:
If you can get them I would use IDs or Names for the elements you are
trying to find. Hopefully those will be much cleaner than the xpath.
You are rerunning your find element multiple times when you could
just use a WebElement and make your code far more readable.
I would try to use Explicit waits vs the implicit ones.
So this is my best suggestion of a starting point based on the code you provided.:
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement searchID = driver.findElement(By.xpath("ctl00$ContentPlaceHolderView$ctlRegistration$DropDownList1"));
List<WebElement> searchOptions=searchID.findElements(By.tagName("option"));
for(WebElement option : searchOptions)
{
if(option.getText().equals("drop down name"))// or since you were looking for the index of 1 regardless you can just do just to a .get(1) from the collection directly.
{
wait.until(ExpectedConditions.visibilityOf(option));
option.click();
WebElement textbox = driver.findElement(By.name("ctl00$ContentPlaceHolderView$ctlRegistration$TextBox1"));
wait.until(ExpectedConditions.visibilityOf(textbox));
textbox.sendKeys(this.getProperties(Country+"_"+UserType+"_Registration_User1"));
textbox.click(); //is this supposed to be a submit of some sort?

Categories