Selenium WebDriver - Facebook Posting Issue - java

I'm really having trouble automating the facebook posting process.
My script is simply unable to track the textarea in our news feed correctly.
I've tried different selectors, even the ones suggested by Selenium IDE but got no avail.
Here's a snippet of my code:
//xpath for status update box
WebElement statusUpdateBox = driver.findElement(By.xpath(DemoTestData.XPATH_STATUSUPDATE_BTN));
//click the status update box
driver.findElement((By.xpath(DemoTestData.XPATH_STATUSUPDATE_BTN))).click();
//input data in the status update box
statusUpdateBox.sendKeys(DemoTestData.DATA_STATUSUPDATE_MSG);
WebElement postButton = driver.findElement(By.xpath(DemoTestData.XPATH_STATUSUPDATE_BTN));
//click 'post' button
postButton.click();
The xPath for
DemoTestData.XPATH_STATUSUPDATE_BTN = "html/body/div[1]/div[2]/div[1]/div/div[2]/div[2]/div[2]/div/div[3]/div/div/div[2]/div/div/div/form/div[1]/div/div[2]/textarea";
What am possibly missing?
Thanks a lot. :D

Here is what I've found. When I checked your xpath in my FB page it returned no results. (a very handy tool is a plugin called free simple firepath in firefox, be sure to get it if you don't!)
It seems unlikely that the button has an xpath ending with "textarea" are you sure that is correct?
When you run your test does the text get filled in? If not, the (first) problem is probably the findElement of the textbox.
When I looked some more I found that the css is actually very simple "textarea" . So if you'd use driver.FindElement(By.CssSelector(“textarea”))
that should return the text area.
The button has a bit of a strange css (to me, but I am quite new to this as well), but I beleieve you could find that by the text in it. In my language it says "Plaatsen" It might say something different for you. Check in Firepath!
The something like driver.FindElement(By.XPath("//*[text()[contains(.,'Plaatsen')]]")) should return the button.
I hope it helps! And I hope you found this answer useful, any tips on that would appreciated, it my first time answering something on the mighty Stack Overflow!
Cheers!

Related

POP up appears without clicking the Link

I'm automating this site:
When I click a button a pop up is displayed. There is a link on the screen to display the pop-up but I don't have anything in my code to click the link this code is commented out.
//Please click to confirm you read and accept the conditions
driver.findElement(By.id("assumptionsConfirmed1")).click();
Please try with xpath i think this might work for you bcz i have seen in resources and xpath look more better than id.
driver.findElement(By.xpath("//a[contains(text(),'assumptions and terms and conditions before procee')]")).click();

Can't inspect search button in IntelliJ-selenium Chrome driver

I am working on automation testing website www.benjerry.com/.
I can't inspect serch button.
I succesfully enter credential in search field, but can't click on search button.
I try all kind of combinations, but still nothing.
Can someone please help me?
Picture with marked problem
Code line:
driver.findElement(By.cssSelector("div.btn-submit")).click();
As you have already accepted the answer given from #kushal. I'd like to highlight some points that could be useful in your Automation Journey .
You can use this cssSelector : input[id='searchTerm'] ~button>span
your code would like this :
driver.findElement(By.cssSelector("input[id='searchTerm'] ~button>span")).click();
Here are some benefits of using this cssSelector :
cssSelector are more consistent than Xpath.
Xpath Provided by Kushal contains numeric that is [2], which is never gonna be consistent , your test may fail if Dev would put one more span inside Button Tag. So in case if you still want to go with Xpath , then your Xpath should look like this :
//input[#id='searchTerm']/following-sibling::button/span
You can find the difference between cssSelector and Xpath here :
What is the difference between css-selector & Xpath? which is better(according to performance & for cross browser testing)?
Try following line of code:
driver.findElement(By.xpath("(//button/span[contains(.,'Search')])[2]")).click();

Unable to click on a button which is auto-focused by a script

I'm trying to click a button which is auto-focused by a script in the html.
I've tried both absolute path, relative path but no luck. pretty much anything I can think of. Attached is a screenshot which will display that the xpath is locating the element successfully on the web but when I use it in selenium, it can't find the element.
I've also tried waiting explicitly for 10 seconds for the element using a generic function. (Check the screenshot) This function is working for all other buttons that were called previously except for this one.
Something CAUGHT my eyes is that There's a method being called which auto-focuses the button. I might have to turn the focus to the entire window or the page in this case. I've also tried sending the enter key but still no luck.
Question_1: Does anyone have any solution?
Question_2: Does anyone know how to switch focus to the page?
Question_3: Or anything.
Any suggestion is highly appreciated.
Use Java Script click instead of click. Like this, Hope this will help.
WebElement element = driver.findElement(By.id("SubmitButtonId"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
To switch focus to parent window/default content window you should use switchTo() method on driver instance.
driver.switchTo().parentFrame();
OR
driver.switchTo().defaultContent();
You can use following two statements to know which element is currently focused.
driver.switchTo().activeElement();
driver.getPageSource();
If you try to click on input tag , your script will fail. try to find correct locator only. it will help or try submit() function as you said, this element is auto selected.
I thinks you need to bring element in to view before clicking on the same:
the code for that is:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true)", webElement);

getting the text from an WebElement

Say I have an element (doesn't really matter what. Let's say "//div[#id='tony']/div/span" but any xpath would be OK.
To get the text on a page from this you could do something like
WebElement ele = driver.findElement(By.xpath("//div[#id='tony']/div/span"));
System.out.println(ele.getText());
// Or an our system someone wrote a method so we can get all text in that
// element and everything underneath (part of that element)
System.out.println(getText(ele));
This is fine when you are running the program. But originally when you are writing and debugging with Chrome before you actually write the program. In Chrome you can do right click -> inspect and then in the bottom do a ctrl-f which opens a search box and you can type an xpath and it will highlight it (them) on the page.
Now I swear several months ago when I was debugging I could also have it display the text in that element but I don't remember how? You type something into the search box like get-text(//div[#id='tony']/div/span) but I can't remember or figure it out. Perhaps at the time I was using Firebug and not Chrome?
Does anyone know a way to have it print the text of the element? I know you can eyeball it in the HTML code but what you think is the text and what it actually is is not always the same

Selenium Webdriver - Cannot click button - Finds element

Hopefully someone can help me with a solution to my problem. I have spent the better part of the day trying nearly every solution I can find on here and through Google.
I will cut to the chase. I have a test that logs in on one screen. The second screen selects a role and moves on. I am able to get to the role selection screen, but cannot move forward as Selenium will not click the button, regardless of what I do.
The interesting thing is that this will work with the IDE, but exporting the code to java and running it with the webdriver does not work either. This part makes no sense to me as I would think the IDE export should run easily. The only difference I can see is that the webdriver pulls up a profile-less Firefox, whereas the IDE is running on my profile - not sure that this would have anything to do with it, but wanted to throw that out there.
The only difference between the first button and the second button is the autofocus command in the HTML.
<button data-submit-button="" class="btn" autofocus="" name="submit" type="submit">Go</button>
I've tried numerous solutions found throughout the web and the last thing I've tried is
new WebDriverWait(driver,120).until(ExpectedConditions.elementToBeSelected(By.name("submit")));
driver.findElement(By.name("submit")).click();
This does not work either. I have tried several variations on this and most of the time the webdriver either times out or skips through this step, passing it and then failing on the next steps as the page has not moved forward.
Does anyone have any suggestions? If you have any questions or I can give you more information if I haven't given enough! Any help with this would be amazing, as I've spent the better part of the day trying to get through this one issue.
Thanks!
Sometimes the selector is the main issue in the scenario like this. Also, name="submit" is not even a very safe selector to use since developers have tendency to duplicate the same attribute across the form. Without knowing about the actual elements I can pretty much tell you the issue is the selector. If the text Go is the only text of the button on that page, give the following xpath a shot
//button[#name='submit'][#type='submit'][contains(text(),'Go')]
Complete implementation should look like as follows:
By xpath = By.xpath("//button[#name='submit'][#type='submit'][contains(text(),'Go')]");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(xpath));
myDynamicElement.click();

Categories