Issue with clicking link on selenium java - 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.

Related

Is there a better way to programatically select a value from select2 dropdown using selenium WebDriver?

I am using Selenium WebDriver to automate something. It requires filling a form that involves selecting a value from a select2 dropdown. This is the code snippet that I am using-
final By SELECT_DIV = By.id("s2");
click(SELECT_DIV);
final By INPUT = By.cssSelector(".select2-drop-active .select2-input");
waitForVisibilityOfElement(INPUT);
enterCharSequence(INPUT, "someData");
waitForJSandJQueryToLoad(30);//30 seconds
final By LIST_ITEM = By.cssSelector(".select2-drop-active ul.select2-results li.select2-result-selectable");
click(LIST_ITEM);
FYI, there are no unique ids assigned to some of these elements and hence I used css selectors for locating them.
This code works but it sometimes throws a StaleElementReferenceException. This is the error:
org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Selenium version : 2.53
So, I want to know if there is any way I could avoid this. I read a few posts about it but they were not of much help.
Let me know if you need more information. Any help would be appreciated.
'StaleElementReferenceException' means that the element has changed. It is in another div, another span or the its properties changed. Selenium may found it but it changed the very second you tried to click on it.
You need to search for the same element again and wait for it to be clickAble or visible. For example:
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement button =
wait.until(ExpectedConditions.
elementToBeClickable(By.id("btnLogin")));

How can I select below element in Selenium Java Web Driver?

Well I have below code
<button class="jobs-search-box__submit-button artdeco-button artdeco-button--3 ml2" data-ember-action="" data-ember-action-689="689">Search</button>
I want to find this element in selenium and perform click action. I tried several options like by class, xpath, name, text, contains but nothing worked.
Can someone guide me here?
driver.findElement(By.xpath("//button[contains(.,'Search']")).click();
driver.findElement(By.className("jobs-search-box__submit-button artdeco-button artdeco-button--3 ml2")).click();
driver.findElement(By.className("//*[#id=\"ember689\"]/button")).click();
driver.findElement(By.linkText("Search")).click();
To summarize what was in the comments. Each locator had something off.
By.xpath("//button[contains(.,'Search']")
was missing a parenthesis and needed to be:
By.xpath("//button[contains(.,'Search')]")
Meanwhile, because By.className expects a single className
By.className("jobs-search-box__submit-button artdeco-button artdeco-button--3 ml2")
also does not work. (see github.com/seleniumhq/selenium/issues/1480
but could as:
By.cssSelector(".jobs-search-box__submit-button.artdeco-button.artdeco-button--3.ml2")
Also
By.className("//*[#id=\"ember689\"]/button")
refers to an id not presented (Also, I'm not sure, but I think would need to be by xpath).
By.linkText("Search")
does not work because there is no tag a and so no hyperlink.
In Protractor this is much simpler because you would just say by.buttonText('Search')
You can achieve the same things by using javascript. kindly find the below example of code:
//Creating the JavascriptExecutor interface object by Typecasting
JavascriptExecutor js = (JavascriptExecutor)driver;
WebElement button =driver.findElement(By.xpath("//button[#class='jobs-search-box__submit-button artdeco-button artdeco-button--3 ml2']"));
//Perform Click on LOGIN button using JavascriptExecutor
js.executeScript("arguments[0].click();", button);
I hope it will work on your case.
Note: Make sure your element will be static.
The correct XPath locator would be:
//button[text()='Search']
If you won't be able to locate it using the above query, make sure that:
The button doesn't belong to and <iframe>, if this is the case - you will have to change the context using switchTo() function
The element is present in DOM, i.e. the page has been loaded fully. It's better to use Explicit Wait for element location/interaction like:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Search']")));
More information: How to use Selenium to test web applications using AJAX technology
Try with These two hope it works,
1.) Using Contains
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Search')]")));
2.) Using CSS
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.jobs-search-box__submit-button artdeco-button artdeco-button--3 ml2")));
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.contains('Search')")));
If does not work let me know i'll provide another Solution.

Can't Select Checkbox, Selenium WebDriver

I am unable to select a checkbox with Selenium WebDriver in Java.
I tried by Xpath but no result.
WebDriver can't click on element.
I tried with Selenium IDE - recorder, no results.
Here it is - html code for checkbox
I try:
1.
driver.findElement(By.xpath(".//form[#id='placeOrderForm1']/div[#class='terms right']/label")).click();
2.
driver.findElement(By.id("Terms1")).click();
3.
driver.findElement(By.cssSelector("label")).click();
4.
driver.findElement(By.xpath("//div[3]/form/div/input")).click();
Nothing works.
Please help.
Try using JavascriptExecuter Hope this will help
WebElement element = driver.findElement(By.id("Terms1"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", element );
Your code seems correct. Specially this one -
driver.findElement(By.id("Terms1")).click();
It might be possible the element you are clicking is not visible in the page scroll. Try to move to the element first and then click.
Try with this -
WebElement elem = driver.findElement(By.id("Term1"));
Actions action = new Actions(driver).
action.moveToElement(elem).click().build().perform();
Hope this help.
Here is the Answer of your Question:
As you mentioned unable to select a checkbox, actually we don't select the checkbox, we checkmark the checkbox. The checkbox you depicted have an id as Terms1 and name astermsCheck`. So you use either of the locators to checkmark the checkbox as follows:
driver.findElement(By.id("Terms1")).click();
OR
element = driver.findElement(By.name("termsCheck")).click();
Let me know if this Answers your Question.
You can find the element by a unique identifier. In this case, we can use name or id. The better choice is to go with id.
WebElement element = driver.findElement(By.name("termsCheck"));
element.click();
or you can use this one also
driver.findElement(By.id("Terms1")).click();

How to click on a object or fire event in Selenium

I am facing a strnge issue, when i am trying to click on "C" link displayed in the attachment. Please observe the link description marked in Red.
I tried below it didnt helped.
WebElement ele=driver.findElement(By.xpath("//ul[#class='whp-rolodex']/li[contains(.,'C')]"));
ele.click();
Then i tried with Actions, but didnt help
Actions action=new Actions(driver);
action.moveToElement(ele).click().perform();
action.click().perform(); //this also didnt help
Then tried with JavaScriptExecutor, this also didnt help
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
I kept ele.isDisplayed() before these clicks and everywhere it shows true.
Can someone help where i am going wrong, are there any better ways.
Using FF 40, selenium webdriver 2.47
Its because your xpath is wrong. C contains in "a" tag while you are trying to find c in "li" tag.
use xpath something like this
//ul[#class='whp-rolodex']/li[3]/a[contains(.,'C')]
While #Shubham's point is perfectly valid, I would simplify it down to using a "link text" locator:
driver.findElement(by.linkText("C"));
which looks quite simple and readable.

Selenium doesn't recognise xpath

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"));

Categories