Selenium By.linkText issues - selenium

So i am automating few tests for this one page, and using method "linkText" for finding element does not work even though it is clearly there,selector is written properly,element is visibile etc. Using its xpath does work however.
Any similiar experiences , ideas what may be the cause?
[Update]
WebElement element = driver.findElement(By.linkText("Sign up/login"));
<li class="n15-menu-arrow pull-right">Sign up/login

I see, can you please try partialLinkText?
WebElement element = driver.findElement(By.partialLinkText("login"));
or
WebElement element = driver.findElement(By.partialLinkText("Sign"));
If you locate a wrong element, that means in your HTML there are other elements happen to share this partial link text in their attributes, in that case, I recommend using Css Selector.

Related

Interact with pseudo-elements with Selenium Webdriver?

I am working with Selenium Webdriver with Java.
And I was trying to interact with anchor tag which is enclosed as pseudo element ::before
But I am unable to interact with anchor element.
Here is the screenshot of the HTML structure.
With
JavaScriptExecutor, I understand, we can fetch the propertyValue using window.getComputedStyle().getPropertyValue() but I am not sure, how to interact with <a> element and execute a Click.
Initially, I attempted to click on the Anchor Element without considering the pseudo element as simple Element Interaction.
To fetch the Element:
private By tabRawView_By_CSS = By.cssSelector("[tabid='raw-view'][role='tab']");
But this piece is not throwing any error but it is also not clicking on the element.
Then I thought of using JavaScriptExecutor and was trying to run first in Developers Tool as below image but couldn't find suitable options.
Can anyone please suggest?
If it is Java bindings and all you want to do is to click on an achor tag which has Raw view as a text.
You could try with ExplicitWaits :
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Raw View"))).click();

How to locate elements in AG-Grid? if there is no ids available

I’m facing a challenge in locating the elements in AG Grid. As I unable to locate the elements in the grid. Can we create the user define locator in selenium to locate the elements in AG Grid. Or Kindly suggest any other alternative to work with it. And it is prohibited to use either of the locators xpath, css selector, name, class in our project.
One more thing, is it possible to .
Kindly refer to the attached screenshots for the reference. Do let me know if you need any further information from my side.
Any help would be highly appreciated.
I tried locating the elements with other locators
below xpath should work,
//div[#col-id='locationName'][contains(text(),'Opthamology')]
If you cannot use anything but id - you should ask your application developers to add unique identifiers for each element.
If for some reason it is not possible - you can use WebDriver.executeScript() function which allows executing arbitrary JavaScript code which in its turn can evaluate various selector expressions, for example XPath, the relevant syntax for your case would be something like:
WebElement someElement = (WebElement) driver.executeScript("return document.evaluate('//div[contains(text(),\"Opthamology\")]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;");

WebElement is identified through xpath in the developer console but the element wont be identified through selenium findElement method

The html element Im trying to locate is the "Shared" link.
I wrote a dynamic xpath to locate the element and it shows as the element was identified in the developer console.
But when i use the xpath that i wrote in the developer console to locate the element using selenium, it does not locate the element.
The method i used to check if it locates the element is shown below.
I could not figure out why this issue occurs, Is it because of a issue in the xpath that i have written or because of another issue?
Code you can try out is :
new WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.xpath(" your Xpath ")));
driver.findElement(By.xpath("your Xpath")).click();
The Xpath you have written would work if only one title is present on current page.
driver.find_elements doesn't have attribute to click() so use driver.findElement instead of Elements

How to identify button without Xpath in selenium

Code is below
<button type="submit" class="login-button">Login</button>
In selenium I tried below code:-
driver.findElement(By.classname("Login")).click();
please help me in this code without Xpath
Your classname is login-button not Login
driver.findElement(By.classname("login-button")).click();
You can use also partialLinkText
driver.findElement(By.partialLinkText("Login")).click();
partialLinkText is looking the Sub-String on HTML DOM
You can use also linkText
driver.findElement(By.linkText("Login")).click();
LinkText is looking the same String on HTML DOM
Using CSS-Selector
driver.findElement(By.cssSelector("button[class='login-button']")).click();
Hope it will help you :)
I always Prefer Cssselector rather than Xpath, it's up to the user to choose which they want and what they are comfortable with finding element.
The below link will be very useful if you want to know about CSSSELECTOR.
http://www.w3schools.com/cssref/css_selectors.asp
driver.findElement(By.cssSelector(".login-button")).click();
My suggestion would be Please inspect element and open console
$('.login-button')
Try this one until you get the required element you want. In that way you will be more flexible in getting the most required element.

SVG and selenium

Hej Guys
I an using google visualization api to draw stacked bar chart. Its all fine but now i want to test it with selenium but having a hard time finding the elements in the google chart.
For example i want to click on the chart element but everytime i try to find an element by xpath i get exception "OpenQA.Selenium.NoSuchElementException: The element could not be found"
I read that with selenium its tricky to click on the svg images.
Is there anybody who know a solution cuz i m kind of desprate and i havent find a suitable solution on the net by myself.
My chart looks like this:
http://i48.tinypic.com/21o4swx.png
What i am trying todo is:
webdriver.Navigate().GoToUrl("http://localhost:59777/Tests/TestsMainView");
IWebElement element = webdriver.FindElement(By.XPath("/html/body/div/div[2]/div[2]/iframe/html/body/div/svg/g[2]/g/g[2]/rect[5]"));
Actions myAction = new Actions(webdriver);
myAction.Click(element).Perform();
Thread.Sleep(9999);
Thanks :)
Here is some advice that may help you. First your second line of code is should be
WebElement element = webdriver.FindElement(By.xpath("//img[contains(#src,'http://i48.tinypic.com/21o4swx.png')]"))
You had "IWebElement" (this was probably just a typo in your question). I changed the way the element is found searching for matching element instead of starting at the top level and working down. This is a better practice to narrow down the element you are attempting to interact with so that changes to the code don't instantly break your test. Also unless you start an xpath expression off with "//" or "xpath=" selenium Webdriver will not recognize it so even if your path never changed selenium Webdriver wouldn't be able to find it.
If I understand what you are trying to do then you can also remove lines 3-5 and replace them with the following,
element.click();
This will have selenium Webdriver zoom in on the chart provided by your link. I hope this helps
I noticed xpath cannot select anything within an svg tag. I managed to find elements using className or tagName selectors instead or you could even try cssSelectors but I am not sure about that one. Note that you can still use xpath to access parents of a node inside an svg using:
By.xpath("..");
Hope that will help.