Selenium Click on non button element - java

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.

Related

How to click on a button in a Alert?

i have to automate this page using java ,and selenium.
I must click on the link which has label terms and conditions it shows up a box, and then i need to navigate it down, and click on Agree button.
I have already tried:
driver.click("//*[#id=\"field_terms\"]/td[2]/div/div/label[1]/a"); // Click on the link
it opens the box for me, but i stuck after it.
I have 2 problems:
How to scrol down the pop up?
How to click on the Agree button?
Update:
Regarding the scroll problem i used the below method which does not work:
public void scrollDown() {
JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("arguments[0].scrollTop = arguments[1];","window.scrollBy(0,450)", "");
}
Try using the CSS Selector to locate the I Agree button
'div[class="ui-dialog-buttonset"]>button[btnclass="primary"]'
It worked in my system. I am not a Java person, this is the code I wrote in python for your reference
driver = Chrome()
driver.get('https://signup.insly.com/signup')
terms_and_conditions = driver.find_element_by_link_text('terms and conditions')
terms_and_conditions.click()
import time
time.sleep(2)
i_agree = driver.find_element_by_css_selector(
'div[class="ui-dialog-buttonset"]>button[btnclass="primary"]'
)
i_agree.click()
Let's start with the scroll: just use JavaScript...
JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("window.scrollBy(0,450)", "");
Or you can just scroll up to the element needed...
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", your_WebElement);
For the click 'I agree' use XPATH like this:
"//*[text()='I AGREE']" then just preform click()
Hope this helps you!
Here is the code in Java [Tested & worked].
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/src/drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://signup.insly.com/signup");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.linkText("terms and conditions")).click();
//Click the I AGREE button
WebElement btnIagree = driver
.findElement(By.xpath("//button[#btnclass='primary' and contains(text(),'I agree')]"));
btnIagree.click();
//Verify the check box for T&C is checked
WebElement chkbxTnC=driver.findElement(By.xpath("//*[#id='agree_termsandconditions']/../span[#class='icon-check-empty icon-check']"));
Assert.assertTrue(chkbxTnC.isDisplayed());
The problem is, as soon as you click on the T&C link, it takes few seconds to load the page and since a wait is needed before hitting the I AGREE Button. Apparently, the I AGREE Button is enabled and clickable without the need of scrolling down, so scrolling is not needed here.

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 mark the elements in Div pop up in chrome while using Selenium automation

While automating in automationpractice.com,
Steps:
1.open "automationpractice.com/index.php?id_product=5&controller=product#/size-s/color-orange"
2. click "Add to Cart" button
3. Below add to Cart successfully pop-up displayed(Refer to below screenshot)
Now I cannot mark any elements in this pop up. I tried XPath which is inside the parent window with Div tag, but The error is ElementNotVisible. I tried it with driver.getWindowHandles(), but it shows that only 1 window exists(Parent window). Also tried with alert, but it says no alert exists.
This issue only occurs in the Chrome browser, for firefox , it works fine using xpath inside parent window with Div tag.
Below is the script I tried, can anyone kindly help on this? Thank you in advance!
The xpath for this pop up is "//*[#id="layer_cart"]/div[2]"
Set<String> windows = wd.getWindowHandles();
System.out.println("windows.size():"+ windows.size()); // the result is 1
There is no new window or alert on this page, only problem is your XPATH cotains " instead of ', alo need to wait a few seconds for that element to be visible:
WebDriver driver = new ChromeDriver();
driver.get("http://automationpractice.com/index.php?id_product=2&controller=product");
driver.findElement(By.xpath("//form[#id='buy_block']//p[#id='add_to_cart']")).click();
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#id='blockcart_caroucel']/li[1]")));
driver.findElement(By.xpath("//*[#id='blockcart_caroucel']/li[1]")).click();

Unable to click on pop up screen

After entering values to few fields i click on submit button which produces a pop up screen where i should click go button. I tried below code, it worked once but not working now. Please help
WebDriverWait wait = new WebDriverWait(driver, 6);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='lets_go']")));
driver.findElement(By.xpath(".//*[#id='lets_go']")).click();
How to fix ?
If it's an alert the use:
driver.switchto().alert().accept().
If pop up is a window, then first switch to that window using WindowHandler, then click on element
ExpectedConditions.visibilityOfElementLocated is used for checking that an element is present on the DOM of a page and visible, if this condition true within given time limit, it returns WebElement otherwise throws TimeOutException, So There is no need to find element again, omit last line and try as below :
WebDriverWait wait = new WebDriverWait(driver, 6);
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("lets_go")));
el.click();
I would suggest pasting the snippet of code for that "popup" to get your code working along with stack trace of error you are getting.
Also, you can go through http://www.softwaretestinghelp.com/handle-alerts-popups-selenium-webdriver-selenium-tutorial-16/. It gives you clear idea when it comes to handling popup.

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.

Categories