How to click on a object or fire event in Selenium - java

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.

Related

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.

Selenium: Clicked Element changes depending on number of instance launched

So I'm just trying to get the value of a input textbox after clicking a radiobutton related to it. I have 3 radiobuttons, let's call them "radio1" "radio2" and "radio3"
By default, "radio3" is checked, my goal is to click "radio1" and then get the value of the textbox. Here is the code I'm running:
WebElement radioMonthly = driver.findElement(By.xpath("//*
[#id=\"dateFromTo_date_format_2\"]"));
Actions actions = new Actions(driver);
actions.moveToElement(radioMonthly).click().perform();
At first attempt, "radio1" is clicked and the value stored properly, but if I launch the test again, it's "radio2" the clicked one. Note that they don't share id.
I already fixed the issue but I would like to know what's going on here
Thanks
Here is how I fixed it, following this thread on SO
Debugging "Element is not clickable at point" error
And the code:
WebElement radioMonthly =
driver.findElement(By.xpath("//[#id=\"dateFromTo_date_format_2\"]"));
JavascriptExecutor clickradioMonthly = (JavascriptExecutor)driver;
clickradioMonthly.executeScript("arguments[0].click()", radioMonthly);
But still will be great if someone is able to understand and explain whats going on the question
Thanks

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.

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

Selenium Click on non button element

I Want to click on a Facebook Post Like button using Selenium and Java With no Luck .
Here is the code:
driver.navigate().to("https://www.facebook.com/DavidGuetta/posts/10153196593446356");
driver.findElement(By.xpath("//a[contains(.,'Like')]")).click();
But sadly it does not work ..
i've tried this too
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(.,'Like')]"))).click();
with no luck , just do not know why the button is not clicked , please help .. and thanks :)
You can try cssSelector below. It works on my machine.
By locator = By.cssSelector("a[data-testid='fb-ufi-likelink']");
driver.findElement(locator).click();
If it doesn't work. Please give me the exception message.

Categories