How to select options provided in a textbox using Selenium - java

Link: http://www.bbc.com/weather/
Scenario: Type "reading" on the find a forecast text box. This shows 2 options. How do I select one option using Selenium WebDriver?
I am using the following command to type "reading"
driver.findElement(By.id("locator-form-search")).sendKeys("Reading");

I added explict wait as you suggested and this worked with a small change in the css path. This is what I did:
WebElement suggestedList = new WebDriverWait(driver, 15)
.until(ExpectedConditions.elementToBeClickable(By
.cssSelector("div[class='locator-suggestions locator-suggestions-default'] >ul >li:nth-of-type(1)")));
driver.findElement(
By.cssSelector("div[class='locator-suggestions locator-suggestions-default'] >ul >li:nth-of-type(1)"))
.click();

Use the following locator to click on the first element (first suggestion displayed after typing):
driver.findElement(By.cssSelector("div[class='locator-suggestions locator-suggestions-default'] li:nth-child(1)")).click();
Similarly you can click on the other element by changing the locator. For example to select the second element:
driver.findElement(By.cssSelector("div[class='locator-suggestions locator-suggestions-default'] li:nth-child(2)")).click();
Just to add here, you may need to wait for the elements/suggestions (which you're trying to select) to be visible, before trying to click on those. Use/search for a appropriate wait method for your testing.
So,
first you need to type "reading" in the field as you are doing by using driver.findElement(By.cssSelector("input#locator-form-search")).sendKeys("Reading");
then wait for element to be visible
Then try the above mentioned code to select the desired element.
Hope this helps.

Related

Selenium not able to input element in the form inside fieldset tag

I have tried a different method to input elements like
Here is xpath I used
By NameOfTheProperty=By.xpath("//fieldset/div/input[#name='name']");
By NameOfTheProperty=By.xpath("//div/input[#name='name']");
By NameOfTheProperty=By.xpath("//input[#name='name']");
Tried with Selenium Builder
WebElement element=driver.findElement(by);
Actions builder = new Actions(driver);
Action mouseOverHome = builder
.moveToElement(element)
.click().sendKeys(text).build();
mouseOverHome.perform();
Tried with
WebElement element=driver.findElement(by);
element.sendKeys(text);
None of the methods is working..I can not able to input text inside the field and It shows
Element not interactable
Here is the site
I hope someone help me to find out the solution..
Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
xpath you should checks is :
//input[#name='name']
if it is unique, then you can use Javascript executor :
WebElement password_input = driver.findElemenet(By.xpath("//input[#name='name']"));
((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('value', 'password_should_be_written_here')", password_input)
personally I would use id's if the element has one.
"//*[#id='__BVID__104']"
Make sure you are waiting for the element to be ready.
WebDriverWait wait = new WebDriverWait();
wait.until(ExpectedConditions.elementToBeClickable(element));
If it then times out on wait.until(... then I've found that sometimes an element must first be clicked for it to be exposed.
If this is the case. You will have to inspect the element before it is clicked. Find it's xpath, and see if it changes when it is clicked. If so then create a webElement for that element and have Selenium first click it, before clicking the actual field/element.

JAVA - Hidden Class Selenium

I am a beginner in selenium and I would like to press a file submission field.
I have already done a whole code to connect to the page, click on the buttons etc. (everything works, my driver is good)
But impossible to click on adding file
I looked on the internet how to do it, I added time, tried to browse the frames, used javascript for the hidden class... I tried all the buttons in the field and it doesn't detect them.
Add File
Source code
Thread.sleep(2000);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,1000)");
WebDriverWait wait = new WebDriverWait(driver, 60);// 1 minute
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*#id=\"yui_3_17_2_1_1584634673387_348\"]/div[1]/div[1]/a")));`
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="yui_3_17_2_1_1584634673387_348"]/div[1]/div[1]/a"}
Do you have an idea ?
The primary issue I observed looking at the code is the incorrect locator in your Explicit Condition element checking line. It can be replaced with below code:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id='yui_3_17_2_1_1584634673387_348']/div[1]/div[1]/a")));
Basic XPath Syntax for reference though it has it's variations:
//tagname[#attrbute='value']
Additional advice Though I am not sure about the application you are automating, but the ID is likely to be changed. Based on the DOM Structure you have provided in the link above I would say change the locator to something on the lines of:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[#role='button'][#title='Add..']")));

How to click on the reCaptcha checkbox

I have an issue with located correct Xpath/ID input field, I am taking the following website as an example:
https://garden.lovetoknow.com/vegetable-garden/how-ripen-green-tomatoes-off-vine
After you open the link then scroll down a bit, you will see "write a comment" blue button, click it then fill the text and name, it will appear a reCaptcha example, I tried the following way to click the checkbox, but without success. I would really appreciate if someone can let me a hand on it
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath(
"//iframe[starts-with(#name, 'a-') and starts-with(#src, 'https://www.google.com/recaptcha')]")));
new WebDriverWait(driver, 10)
.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.recaptcha-checkbox-checkmark"))).click();
Seems you were close enough. The name of the <iframe> i.e. a-x2cp4vfbaqu8 looks dynamic, so it would be better to avoid the name attribute and you can use either of the following Locator Strategies:
Using css_selector:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("iframe[src^='https://www.google.com/recaptcha/api2/anchor']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span#recaptcha-anchor"))).click();
Using xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(#src, 'https://www.google.com/recaptcha/api2/anchor')]")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("//span[#id='recaptcha-anchor']"))).click();
References
You can find a couple of relevant discussions in:
How to click on elements within an iframe to enable the captcha through the audio using Selenium and Python
Find the reCAPTCHA element and click on it — Python + Selenium
Dealing with reCAPTCHA in Python Selenium
I struggled with this for several days, it's actually very simple. The captcha is located in a frame, and in order to click on it, you need to switch to this frame.
I did it like this with Selenium:
driver.switchTo().frame(driver.findElement(By.xpath("//*[#title='reCAPTCHA']")));
driver.findElement(By.xpath("//span[#id='recaptcha-anchor']")).click();

I am unable select checkbox in dropdown using selenium webdriver

It looks like this
id("ctl05_cbMulti_DropDown")/div[#class="rcbScroll rcbWidth"]/ul[#class="rcbList"]/li[#class="rcbHovered"]/label[1]/input[#class="rcbCheckBox"]
None of these are working
driver. findElement(By.xpath("(//input[#type='checkbox'])[2]")).click();
or
driver.findElement(By.id("ctl05_ctl03_cblUser_Input")).sendKeys("Smith, John");
Based on your follow up comment, the problem is that the child selector should be 1 as XPath child selectors are zero based, and the additional parenthesis aren't needed. Just change the number to reference the correct Checkbox, for example:
If you want to select the first Checkbox use this:
Using XPath:
driver.findElement(By.xpath("//input[#type='checkbox'][0]")).click();
Another way to do get the checkbox is using CSS Selectors:
driver.findElements(By.cssSelector("input[class='rcbCheckBox']")[0].click();
And if you wanted to select the second Checkbox:
Using XPath:
driver.findElement(By.xpath("//input[#type='checkbox'][1]")).click();
Using CSS:
driver.findElements(By.cssSelector("input[class='rcbCheckBox']")[1].click();
I'm more familiar with CSS so my XPath syntax my not be completely correct. But the idea remains the same.
I see some mistakes in your first line of code. You should not pass "(" inside xpath.
Correct code should be:
new WebDriverWait(driver, 30).until(ExpectedConditions.ElementToBeClickable (By.xpath("//input[#type='checkbox'][2]")));
driver. findElement(By.xpath("//input[#type='checkbox'][2]").click();
check this example in http://simplejavautomation.blogspot.com/p/dropdown-checkbox.html
driver.findElement(By.xpath("//span[#class='hida']")).click();
WebElement mutliSelect = driver.findElement(By.xpath("//div[#class='mutliSelect']"));
mutliSelect.findElements(By.xpath(".//li")).forEach(li -> {
li.findElement(By.xpath(".//input[#type='checkbox']")).click();
});

Why am I not able to click the web element using the following code, even though I can see that the controller is tracking the xpath?

click here to check the page I am working on
The following is the java code:
driver.findElement(By.xpath(".//*[#id='ref_2665398031']/li[4]/a/span1")).click();
The element is actually in the left navigation pane.
Here when this particular statement is getting executed, I can see that browser is moving down, but it does not click that element.
You have not properly written the xpath:
".//*[#id='ref_2665398031']/li[4]/a/span1"
it should be : "//[#id='ref_2665398031']/li[4]/a/span[1]"
and if using WebDriver then
"//[#id=\"ref_2665398031\"]/li[4]/a/span[1]"
After correcting above if still error exists:
You are using a relative path which is relative to the element having id='ref_2665398031.
This id seems to be generated dynamically.
Please confirm two things:
Your code is not giving element not found error.
Page refresh is not changing this id. (If it is being generated dynamically than it will change on page refresh.)
Otherwise try using different approach:
driver.findElement(By.xpath("//*[contains(text(), '50% Off or more')]"));
Use this xpath:
driver.findElement(By.xpath("//ul[#id='ref_2665398031']/li[4]/a/span[1]")).click();
Try Below xpath it is working for me
driver.findElement(By.xpath("//ul[contains(#id,'ref_2665398031')]/li[4]/a")).click();
Try JavaScriptExecutor instead of using normal Selenium click method.
WebElement element = driver.findElement(By.xpath(".//span[contains(text(),'50% Off or more')]"));
((JavascriptExecutor) driver).executeScript("return arguments[0].click();", element);

Categories

Resources