Selenium doesn't recognise xpath - java

I am using Selenium WebDriver to get from a drop down list values. Unfortunately I can't get it, because my code can't recognise the xpath.
Here's my code:
WebElement selector = driver.findElement(By.xpath("id('search')/x:fieldset/x:table[1]/x:tbody/x:tr[2]/x:td[1]/x:select"));
Select s = new Select(selector);
List<WebElement> options = s.getOptions();
for (WebElement wb : options) {
System.out.println(wb.getText());
}
The problem is with the 1st line (WebElement selector). In output I get something like this:
Exception in thread "main"
org.openqa.selenium.InvalidSelectorException: The xpath expression
'id('search')/x:fieldset/x:table[1]/x:tbody/x:tr[2]/x:td[1]/x:select'
cannot be evaluated
I've even tried to find by name or class, but selenium still doesn't find this list.
How to solve the problem? Thanks in advance :)

Ganjira, This is no way to write a xpath. If you find it tough to recognize item . Use Selenium IDE 'Select' button.

If you can provide sample of html page on which you try to find your element, it would be very helpful. Anyway, try to search using css selectors e.g.
WebElement selector = driver.findElement(By.css("#search > select"));

Related

Unable to locate elements on a website , i tried all the locators but I get the error "No such element" . I am a self taught beginner to Automation

Element HTML:
Inbox
What I tried:
driver.findElement(By.cssSelector("a[text='Log out']"));
then
driver.findElement(By.xpath("//a[.='Log out']"));
Element snapshot:
HTML
driver.findElement(By.linkText("Log out"));
Something like that should work, you should provide the page html for a better response.
My response is based on the fact that in your first try you are saying text='Log out'.
findElement in selenium works with locatorStrategies (By.cssSelector/By.linkText...) the one that i used (linkText) search in all the anchor tags that are found in the pages () and analyze the text inside the tag, if the text is matched the driver will find the element.
Let me know if it works, otherwise provide me the web page html snippet.
I've seen the actual screen, you must change Log out with Inbox
driver.findElement(By.linkText("Inbox"));
Given the HTML:
Inbox
You need to take care of a couple of things here as follows:
Within cssSelector doesn't supports the :contains("text") method
Within xpath for exact text matches you need to use text()
Solution
To identify the element you can use either of the following locator strategies:
Using linkText:
WebElement element = driver.findElement(By.linkText("Log out"));
Using cssSelector:
WebElement element = driver.findElement(By.cssSelector("a[href$='INBOX'][title='View the Inbox']"));
Using xpath:
WebElement element = driver.findElement(By.xpath("//a[text()='Inbox' and #title='View the Inbox']"));

Cant find element by xpath to select

Hi im having serious issues trying to locate this element using an xpath:
class="accountSettingsTextBase_1u09j40-o_O-accountSettingsItemText_10y8axf" aria-current="false" href="/account/signin">Log In
I've tried:
"//*[contains(text(),'Log In')]"
and also the actual xpath:
//*[#id='root']/div/header/div[3]/nav/ul/li[1]/a
as well as link text which still won't work, any ideas??
The anchor tags are missing from the HTML by the way, as it was just showing "Log In" with them added in
Full HTML:
Full HTML
Possible xpaths :
//a[#href='/account/signin']
OR
//a[contains(.,'Log In')]
OR
Try using Css selector :
a[class^='accountSettingsTextBase_' ][class*='accountSettingsItemText']
//[contains(text(),'Log In')]
You're missing something:
//*[contains(text(),'Log In')]
Why don't you use
driver.findElement(By.linkText("Log In"))
or
driver.findElement(By.partialLinkText("Log In"))
since the element is an anchor tag?
Please add the explicit wait and then check
WebDriverWait wait=new WebDriverWait(driver,20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(),'Log In')]")));
driver.findElement(By.xpath("//*[contains(text(),'Log In')]")).click();

Issue with clicking link on selenium java

I tried everything. By xpath , by css-selector , by class name too.
//*[#id="opbox-listing"]/div/div/section[2]/section/article[5]/div/div/div[2]/h2/a
thats look xpath , but don't work
on selenium i tried thats way:
driver.findElement(By.xpath("//*[#id=opbox-listing']/div/div/section[2]/section/article[5]/div/div/div[2]/h2/a")).submit();
driver.findElement(By.xpath("//*[#id=opbox-listing']/div/div/section[2]/section/article[5]/div/div/div[2]/h2/a")).click();
what i do wrong? someone have any ideas?
Your xpath is broken: opening apostrophe is missing for an id value:
"//*[#id='opbox-listing']/div/div/section[2]/section/article[5]/div/div/div[2]/h2/a"
Try out clicking on the element using one of following methods
Method 1:
Actions action = new Actions(driver);
action.moveToElement(<your WebElement>).click().perform();
Method 2:
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", <your WebElement>);
These are just workarounds. Please provide the exception that you are getting while performing the normal selenium click operation may be that can help you find an answer.

Selenium in Java: Not able to locate element on Amazon

I am trying to get the text for Rating of any product on Amazon but I am unable to write the correct code. I don't understand what I am doing wrong here. Here its not even able to locate the element.
Also I don't think xpath is wrong because I checked with Firepath.
Below is the code:
public static void main(String args[])
{
System.setProperty("webdriver.gecko.driver", "D:\\Eclipse and workspace\\eclipse\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.amazon.in/");
WebElement elem = driver.findElement(By.id("twotabsearchtextbox"));
elem.sendKeys("Camera DSLR");
driver.findElement(By.className("nav-input")).click();
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='result_0']/div/div/div/div[2]/div[3]/div[2]/div[1]/span/span/a/i[1]/span")));
WebElement elem2 = driver.findElement(By.xpath(".//*[#id='result_0']/div/div/div/div[2]/div[3]/div[2]/div[1]/span/span/a/i[1]/span"));
elem2.getText();
}
Please help me out.
The span element you want to get contains text like "4.4 out of 5 stars", but actually you see just an icon with the stars, so it's bad idea to use visibilityOfElementLocated condition as it won't be visible anyway.
Try to use presenceOfElementLocated instead:
WebElement elem2 = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[#class='a-popover-trigger a-declarative']//span[#class='a-icon-alt']")));
elem2.getAttribute("textContent");
Note that to get text content of invisible span you should use getAttribute("textContent") instead of getText()
Use dynamic xpath instead of absolute xpath:
By.xpath("//*[contains(#class,'a-icon-star')]//*[contains(#class,'a-icon-alt')]")
And as rightly pointed out by #Andersson,
Use presenceOfElementLocated instead of visibilityOfElementLocated as tooltips are designed to be invisible.
Use textContent attribute for invisible elements such as tooltips instead of getText().

Obtain Selenium WebElement from cssSelector over HTML Input

I am quite new on Selenium (started today) and I would like to get the WebElement corresponding to the following html Input:
<input size="25" style="text-align:center;" value="http" onclick="this.select();" type="text"></input>
And then obtain its value. This is what I have tried so far:
WebElement element = driver.findElement(By.cssSelector(".text-align:center"));
String text = element.getText();
Or this:
WebElement element = driver.findElement(By.cssSelector("input[style='text-align:center']"));
But Java returns in both cases an exception:
org.openqa.selenium.InvalidSelectorException: The given selector
.text-align:center is either invalid or does not result in a
WebElement
Thank you,
Héctor
Do you have to search for the element by cssSelector?
You could give this a try:
WebElement element = driver.findElement(By.cssSelector("input[type='text']"));
If cssSelector is not necessary you could try grabbing the element by xpath.
If you use firefox, there is a plugin called FireBug which allows you to right click after inspecting the element and copying the xpath directly then using :
WebElement element = driver.findElement(By.xpath("XPATH HERE"));
EDIT: Part of post disappeared, redded it.
Your first try is slightly off
driver.findElement(By.cssSelector(".text-align:center"));
The (.) in a CSS selector indicates a CSS class name but that's a style on the element and not a class. There is no class on that element to use in that way.
Your second try looks good but maybe it's not unique on the page? Hard to tell with only the one line of HTML. You'd have to provide more of the HTML of the page. Try it again but get the value instead of text.
WebElement element = driver.findElement(By.cssSelector("input[style='text-align:center']"));
System.out.println(element.getAttribute("value"));
Does that work? You likely will have to provide some unique HTML that surrounds the INPUT that we can use to make the CSS selector more specific.

Categories