I'm getting this error while trying to find the web element for testing the facebook "create a page">"sign up" button under Page Object Model. I tried various options like by class name(CSS selector) and also by copying the system xpath but getting the same error message
org.openqa.selenium.NoSuchElementException: no such element : Unable to locate element
/driver.findElement(By.xpath("//*[#id='blueBarDOMInspector']/div/div[1]/div/div/span/a")).click();
driver.findElement(By.className("_42ft _4jy0 signup_btn _4jy4 _4jy2 selected _51sy"));
Looks like you class name generates randomly - you can check it by re-visiting same page from different browsers (or you can try incognito). If it's true you need another way to work with element. Some examples:
If this Sign Up button - use xPath and look for the text in that element
Think if your task can be resolved without using interaction with UI (or part of the task). Read about facebook API and if possible use this instead of UI. It will save you a lot of time and hassle down the road.
Related
I'am using selenium framework to test my own website. Im trying to click on specific icon which using anchor tag. I have java selenium code to click but couldn't click. Tried many xpath, css selectors, class name and names. but didn't worked. But can run the script and it is opening the chrome and navigating to entered domain but the clicking option is not working
above code I need to click nav-twitter class anchor option . which will create another tab in chrome to show my twitter page. but after running the app .it is only navigating to the page domain and nothing works.
So, This is my code where I have added. until the maximize everything works but not the anchor tag
This kind of error im getting when running the script in chrome. Please anyone let me know where I have been wrong here or is there are any way to make it happen. Thanks in advance.
Whenever you are initiating webdriver make sure to add implicit wait statement, so it can wait for sometime before looking for objects. Add below statement right after chromedriver initialization and your code should work without any issues.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Your locator is slightly off. In your code you are looking for an element with tagName as twitter which doesn't exists. Instead you can use either of the following locator strategies:
Using cssSelector:
WebElement navTwitter = driver.findElement(By.cssSelector("a.nav-twitter[name='twitter']"));
navTwitter.click();
Using xpath:
WebElement navTwitter = driver.findElement(By.xpath("//a[#class='nav-twitter' and name='twitter']"));
navTwitter.click();
You have used wrong selector/locator here. "twitter" is not tag name, Tag name is the value from less than symbol to the first space or greater than symbol. Words like div, p, a, span are the tag names.
So here a is the tag name and name, class, href are attribute which you can use while forming xpath or css locators. So you can create multiple locators here:
By using class name attribute
By.className("nav-twitter");
By creating xpath using name attribute
By.xpath("//a[#name='twitter']");]
By creating xpath using class attribute
By.xpath("//a[#class='nav-twitter']");
By creating css locator using class attribute.
By.cssSelector("a[class*='nav-twitter']");
By using dot symbol in css selector with class name
By.cssSelector("a.nav-twitter");
Similarly you can create css selector with name too. Since there are multiple tags, so you can not use a tag directly and If you want to use By.tagName("a") then it will click on first tag present on the page
Note: We use dot with class name and Hash (#) with Id name while forming css selectors.
I'm coding test with Selenium Webdriver (Java), getting https://cloud.google.com as a driver.
I start with finding search input field, sendKeys("search phrase \n"). After that page starts changing its content and I'm trying to intersept these changes with WebDriverWait:
// first Wait - is to wait before page starts changing is content by removing search google icon
new WebDriverWait(driver, 30).until(ExpectedConditions.invisibilityOf(searchInputFieldIcon));
//second Wait - i'm waiting new hyperlink to appear (this hyperlink appears in search results after the whole page is asynchronically reloaded without page reloading)
new WebDriverWait(driver,30)
.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[#href='https://cloud.google.com/products/calculator']")));
The point is, that Wait doesn't waits for 30 seconds before element shows up. Code just throws an exception:
org.openqa.selenium.NoSuchElementException:
no such element: Unable to locate element: {"method":"xpath","selector":"//a[#href='https:`//cloud.google.com/products/calculator']"}`
Any help will be much appreciated!
Please check the attached screenshot. Here the Href link is different then you have used in your code.
You can use the below code
wait.until(expectedConditions.visibilityOfElementLocated(By.linkText("Google Cloud Platform Pricing ")));
To locate the first search result you can use the following xpath;
//a[contains(text(),'Google Cloud Platform Pricing')]
Checking your xpath
You can check whether your xpath is correct or not from the browser itself.
Go to DevTools (Ctril + Shift + I)
In the 'Elements' tab, press Ctrl + F
Input the xpath that you want to check
And it will show you whether it is correct and how many web-elements can be located from it.
I am trying to click a button from a list but this button has the same class than others in the list because they have the same name (btn ban-red) so how can I click it if in the inspect I have this information:
<a class=“btn ban-red” data-track-event=“navigate” data-track=name=“Jobylon” - Quality Engineer” href=“https://emp.jobylon.com/jobs/16654-f/” target=“_blank”>View job/a>
The inspect is copying this xpath:
/html/body/div[1]/div[4]/div/div/div/div[3]/div/div/div/div[1]/section/div/div[2]/div[1]/div[1]/article[14]/a
But it is not working
I also created my own xpath this way:
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#data-track-name=‘Jobylon - Quality Engineer’]"))).click();
But is not working either
I am using Selenium with java and I am in a Macbook, thank you for your help.
Absolute xpath not recommended. You can try using relative xpath.
Locate based on element text
driver.findElement(By.xpath("//a[contains(.,'View job')]")).click()
Locate using combination of element attribute if classes are not unique
driver.findElement(By.xpath("//a[#class='btn ban-red'][#data-track-event='navigate']")).click()
OR
driver.findElement(By.xpath("//a[#class="btn ban-red"][#href='https://emp.jobylon.com/jobs/16654-f/']")).click()
Better to use CSS selector as its faster then xpath. So you try like
driver.findElement(By.cssSelector("a[class='btn ban-red'][data-track-event='navigate']")).click()
OR
driver.findElement(By.cssSelector("a[class="btn ban-red"][href='https://emp.jobylon.com/jobs/16654-f/']")).click()
Still facing some issue like element not visible or no such element then try with explicit wait conditions until your element gets visible or clickable.
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();
Well not to mention that I'm new on using the selenium web driver, I'm trying to automatize a mailbox so I can log myself in and to send a message, the thing is that I download the firebug plugin for Mozilla, that is the browser I'm currently working with, when I'm trying to find the element for the log in and the password everything goes well, because I do it by their name.
input class="_nb-input-controller" type="text" **name="login"** autocorrect="off" autocapitalize="off" value=""
input class="_nb-input-controller" type="password" **name="passwd"** value=""
but as soon as im at the mailbox entry the identificator changes it, i go to compose link and this is the code that shows up:
a class="b-toolbar__item b-toolbar__item_compose js-toolbar-item-compose daria-action" title="Compose (w, c)" href="#compose" data-action="compose.go" data-params="toolbar=1&toolbar.button=compose"
Since I don't find any method that could help on this, a friend came and told me about the xpath which was:
/html/body/div[2]/div/div[5]/div/div[3]/div/div[2]/div/div/div/div[2]/a[2]
by doing this selenium did find the element and I could click on the icon,
but again.. when I'm trying to get into the "TO" prompt so I can write inside
it is not finding the element, not by xpath, or anything is there a way or formula to get elements or translate them into a way that selenium can find them???
oh i see, i re try with the element and i noticed that the one i wanted was showing as hidden using firebug so i just try the next one up and the web driver could locate the item, it was a simple app to send an email the prompt that i wanted to locate was the 'TO' field. and thanks guys for giving me the correct way of using the Xpath property
Having locators such as :
/html/body/div[2]/div/div[5]/div/div[3]/div/div[2]/div/div/div/div[2]/a[2]
is generally a bad idea. If you want to build a stable test I would suggest you stay away from such.
Now I see you have attributes like #name and #class. XPath is provides you with a way to reach these elements directly by pointing the identifiers instead of the whole path to the element.
You can try something like:
//input[#name='login']
//input[#name='password']
More info at: http://www.w3schools.com/xsl/xpath_intro.asp