This is what the error displays when I use the xpath
What command on the selenium webdriver would I use to click a text which has a xpath of
html/body/div[1]/div/div/div[2]/div[2]/div[2]/a
I tried using
driver.findElement(By.xpath("html/body/div[1]/div/div/div[2]/div[2]/div[2]/a")).click();
but still doesn't work... any suggestions?
Your xpath is incorrect. You are trying to hit an absolute path with html/body/div[1]/div/div/div[2]/div[2]/div[2]/a. You are missing a / so instead it should be /html/body/div[1]/div/div/div[2]/div[2]/div[2]/a
try:
driver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/div[2]/div[2]/a")).click();
I have seen similar issues when the element to be located was in a separate iframe.
Using FirePath or source html, check if there are any iframes and if yes, use
driver.switchTo().frame("framename")
and then use
driver.findElement(By.xpath("/html/body/div1/div/div/div[2]/div[2]/div[2]/a")).click();
What about just using By.linkText() or By.partialLinkText() instead of XPath? Using XPath in this way is "easy" but very fragile I would recommend against it.
driver.findElement(By.partialLinkText("EARN up to "));
You can, of course, edit the partial link text to be more or less specific depending on what you are looking for and how unique the text is on the page.
Since that didn't work, let's try a different approach. Let's find all the links in the table you have pictured above. The code below will grab the A tags in the table and then click the first one. My guess from the picture is that the text contained in the link will likely change so having a more flexible way to find the links is better than using the link text. See if the below works.
List<WebElement> links = driver.findElements(By.cssSelector("div.data-block-sub-popular-offers > div.row-heading > a"));
links[0].click();
Instead of using absolute xpath which is not reliable way to identify an element, you can try this: //input[#id='partnerId']/following-sibling::a. It finds an element with unique id and then locates nearest sibling having tag a
just use xpath below:
driver.findElement(By.xpath("//div[#class='row-40percent']/a[contains(text(),'Earn upto 2.5%')]").click();
Related
So I am trying to locate Account Activity from below lines of codes using contains(text(), 'value') but I am not able to do so. I tried to remove   using different stackoverflow answers but none of them work. Can someone please help me out here?
WebPage:
Mylines of code:
Thanks.
Try the following xpath:
//span[contains(text(),'Account Activity')]
From the web page structure you presented here it looks like the Account Activity and Current Day Balances are not inside the input elements but they are all inside the small element which is inside span so I'd prefer basing on span element containing all them.
Try this by normalizing the spaces:
driver.find_element_by_xpath("//input[normalize-space(text()) = 'Account Activity']").click()
If that doesn't work maybe try another approach:
driver.find_element_by_name("irOperations[0]").click()
I have text located in these two locations:
The elements I want the text from:
//*[#id="RoleSetUpNewForm"]/div/table/tbody/tr[1]/td[2]/table[2]/tbody/tr/td/div[1]/table[3]/tbody/tr[2]/td/table[1]/tbody/tr/td[2]/table[1]/tbody/tr/td/label
The elements that I do not want the text from:
//*[#id="home"]/div[16]/form/div/table/tbody/tr[1]/td[1]/table/tbody/tr[18]/td[3]/div/a
This xpath statement when executed in Selenium keeps giving me both the elements I want, AND the elements I do not want:
driver.findElements(By.xpath("//td[#class='level3TD']/label"));
Clearly the xpath above does not reference the location that I do not want the text from, but I am getting the text elements from it returned..
I am just baffled. Any ideas? Is there something I need to do with my selenium xpath query to stop the text from the path ending in /div/a from coming back?
There is no need to manually write XPath's.
Open the dev console by pressing F12 (assuming you are on a Windows machine).
Now find the node that you want, right click on it and click "Copy". Now you can copy the relative XPath, Full XPath, JS Path or Selector. In your case you look like you want the Full XPath.
This can be done on all machines, in all browsers.
I figured this out, there were duplicate label text on the page that made me think the xpath was pulling from the wrong location. Lets close this.
I am not able to select and click on an element which is located in footer.
Below is the code
I tried getting right XPath using Chrome console. Below are the code that I have tried. It is not highlighting the element. Please suggest if why it is not locating.
driver.findElement(By.xpath("li#btnEnableEditing.LoginOkButtonFooter")).click();
And I have also tried using move to as below
a.moveToElement(driver.findElement(By.xpath("li#btnEnableEditing.LoginOkButtonFooter"))).click().perform();
a.moveToElement(driver.findElement(By.xpath("//*#id='btnEnableEditing']"))).click().perform();
The xpath you are using is invalid. The first two code lines are actually used in cssSelector
driver.findElement(By.cssSelector("li#btnEnableEditing.LoginOkButtonFooter")).click();
Or just
driver.findElement(By.cssSelector("#btnEnableEditing")).click();
Or using By.id
driver.findElement(By.id("btnEnableEditing")).click();
And you are missing square brackets in the last one
driver.findElement(By.xpath("//*[#id='btnEnableEditing']")).click();
Click on the element for the below HTML code is not working.
a class="add" href="/travelPlan_revamp/addTravel.htm?travel_type=D">Add</a>
I'm using the next Xpath: html/body/div[1]/div[2]/div/a
But No Such Element Found exception is displayed.
First make sure the web element is not inside iframe.
If yes, Then you need to switch to iframe first and then try clicking on element.
Also, try below xpath, in case you have captured wrong xpath:-
//a[#class='add' and .='Add']
An absolute xpath always starts with a forward slash '/'. I think you have used an absolute xpath and either you have missed'/' or you quoted it wrong here. Please verify it. Normally absolute xpath will look like /html/body/form/div[3]/div[2]/div[1]/div[2]/input
Try this xpath:
//a[contains(text(),'Add')];
Sorry guys the the thing is it was in Iframe. Now i have found the solution by switching to the iframe and then clicking an event.
driver.switchTo().frame("rightMenu");
driver.findElement(By.xpath("html/body/div[1]/div[2]/div/a")).click();
Hard to tell if your XPATH expression is right without the HTML.
Your XPATH expression tell:
Take a "a" elements
In the 2nd div element
In the first div element
In the body element
That is in the html element
So checks there is a "a" element that check the Xpath expression.
//a[#class='add' and text()='Add']
Try that one.
Through which method are you looking for the element?
#FindBy (how = How.XPATH)
or
driver.findElement(By.xpath());
Sometimes the #FindBy notation doesn't like working with Xpaths properly.
Try with this below xpath
//a[contains(text(),'Add')]
how do I get the xpath or css or ANYthing to click this ok button??? Problem is these ids and other attributes have tags that are changed every time a user goes to this particular page
--ea3b2e21-3847-47de-860c-3596695fbb35-- so i don't know what to do
Ok
All you can do here is try and find some part of the element that does not change. If you can add some sample html it would help.
Have you read the
docs for dynamic elements?