How to click on the reCaptcha checkbox - java

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

Related

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 identify the element with text as Sign in with Google through xpath in java selenium

I am performing Automation for a redbus application but I am finding the web elements using firebug and it will highlight the firefox browser but it doesn't work in a chrome browser.
Please review the following screenshots:
Redbus website link
step 1 Open redbus application URL =https://www.redbus.in/
step 2 Click Accounts module
step 3 Click sign or signup link
My XPath is :
.//*[#id='g-signin2']//span[text()='Sign in with Google']
Chrome browser screenshot: Can not identify the webElement
Google sign up link is in Iframe , in order to interact with elements which are inside the frame/iframe , you need to change the focus of your web driver to that particular frame.
How you can do that in your case :
driver.switchTo.frame("//iframe[#class='modalIframe']")
Then you can interact with Sign in with Google.
WebElement signupButton = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(#id,'signed') and text()='Sign in with Google']")))
signupButton.click();
HTH !
To identify and invoke click() on the element with text as Sign in with Google, as the element is within an . So you have to induce WebDriverWait for both the cases, once for the frame to be available and again for the desired element to be clickable and you can use the following solution :
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#class='modalIframe' and #src='/account?pageName=Home&noReload=noReload']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[#class='abcRioButtonContents']//span[normalize-space()='Sign in with Google']"))).click();
What You have here is modal frame, You should first switch on it, and the do actions on elements:
driver.switchTo().frame("modalIframe");
or maybe this:
driver.switchTo().activeElement()
It looks the login popup is available inside the iframe tag. First navigate to that particular frame as below and then add the sign in xpath step.
driver.switchTo().frame(0);

Issue with focus in ie browser using selenium webdriver code

Firstly i need to mouse over and then it opens a drop down then i will click on that link.Its working fine in firefox,chrome and issue is in ie.
Here is the code
WebElement element=driver.findElement(By.xpath("/html/body/div/span/form[2]/div[1]/div[1]/div[3]/div[2]/ul/span[3]/li/a"));
Actions act=new Actions(driver);
act.moveToElement(element).build().perform();
WebElement element2=driver.findElement(By.xpath("/html/body/div/span/form[2]/div[1]/div[1]/div[3]/div[2]/ul/span[3]/li/ul/span[1]/li/a"));
Actions act1=new Actions(driver);
//act1.click(element2);
act1.moveToElement(element2).click(element2).build().perform();
It works even in IE browser, when we use this piece of code
caps.setCapability("requireWindowFocus", true);
If we use requiredwindow focus its working fine even in IE browser so no issues with locators
But am not encouraged to use above requiredwindowfoucs code in my project.
Is there any other way to do it.
The issue in Ie browser when we are not using requiredwindowfocus, it is clicking on some other link, so am assuming the problem is with focus.
so kindly help me with this issue without using requiredfoucswindow
I have has similar issues in my project. In IE, clicking drop down values does not work. I have a pretty weird and unexpected solution for this. Find the element before performing the operations. I am assuming that this is required because when you move to element1 and when trying to find element the IE Xpath processor messes up the focus.
WebElement element=driver.findElement(By.xpath("/html/body/div/span/form[2]/div[1]/div[1]/div[3]/div[2]/ul/span[3]/li/a"));
WebElement element2=driver.findElement(By.xpath("/html/body/div/span/form[2]/div[1]/div[1]/div[3]/div[2]/ul/span[3]/li/ul/span[1]/li/a"));
Actions act=new Actions(driver);
act.moveToElement(element).build().perform();
act.moveToElement(element2).click(element2).build().perform();
Please try this and let me know if this works for you too!!
Also, consider using relative XPaths instead of the absolute XPaths you have used. The second Action is not required. One action should do the trick.
EDIT Based on user comment
Move to element1 is working and the list is getting displayed. So I am assuming that element2 is visible and so we can directly click on it without using the Actions class
Actions act=new Actions(driver);
act.moveToElement(element).build().perform();
element2.click();

How to select options provided in a textbox using Selenium

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.

How can i select text from browser screen with help of mouse events in selenium?

I am trying to select text which is already present on the browser.
I want to select that particular text and perform right click operation on it.
However, the page on the browser has disabled right click.
How can I select text in such situation?
Using a normal web browser without Selenium the only workaround that I can think about is to disable javascript to stop the script that prevents you from right clicking. So it should also work with a browser controlled by Selenium Webdriver.
I don't know if it will be OK for you because your website may be relying on javascript for essential features.
However if you don't need Javascript for your Selenium test you can try the following when you launch your driver :
FirefoxProfile p = new FirefoxProfile();
p.setPreference("javascript.enabled", false);
driver = new FirefoxDriver(p);
I assume that you already know how to perform a right click because your question was only about dealing with the problem preventing you from doing this right click. But if not, you can also refer to this answer :
Select an Option from the Right-Click Menu in Selenium Webdriver - Java
Edit:
I'm sorry I really thought you could use Selenium actions to select the text you want but after some tests I didn't manage to perform a click and drag to select a text. The only thing that works for me in Chrome or Firefox is the following. It looks for a <p>which contains some text and then perform a double click to select a word.
driver.get("http://en.wikipedia.org/wiki/Java_(programming_language)");
WebElement text = driver.findElement(By.xpath("//p[contains(text(),'Java is')]"));
Actions select = new Actions(driver);
select.doubleClick(text).build().perform();
However it only highlighs one word in the html element that contains your text, so it's not really convenient.
I've also tried to do Ctrl+F and type the text so that the web browser automatically select it but the browser doesn't do anything when executing :
Actions search = new Actions(driver);
search .sendKeys(Keys.chord(Keys.CONTROL,"+f")).sendKeys("Java is").build().perform();
It seems that Selenium can only send keys events to the html elements and not to the browser (in the case of ctrl+F).
I don't really see a solution for now, let's see if someone else can find a workaround. It's an interesting issue, it would also be useful for me to select a text the way you described
Move to middle of the element
Actions builder = new Actions(webDriverObject);
builder.moveToElement(element).build().perform();
Move to starting of element, click and hold, move to end
Integer width = element.getSize().getWidth();
Actions newBuilder = new Actions(webDriverObject);
newBuilder.moveByOffset(width/2,0).clickAndHold.moveByOffset(width,0).release().build().perform();

Categories

Resources