Java selenium click element not working - java

I want to click the load More link in a page. My code is below.
pageUrl="http://www.foundpix.com/category/actor/bollywood-actor/"
WebDriver driver = new FirefoxDriver();
driver.get(pageUrl);
driver.manage().window().maximize();
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollBy(0,2500)", "");
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("json_click_handler")));
driver.findElement(By.id("json_click_handler")).click();
How can I make it click the link.

You can use below xpath to click Load More button both the times:-
driver.findElement(By.xpath("//*[#id='blocks-left']/div/div[3]/div[contains(.,'Load More')]")).click();

this button change the location after you click on it and it can be clicked twice so:
before the first click use
driver.findElement(By.xpath("//*[#id="blocks-left"]/div/div[3]/div")).click();
after first click,you can use
driver.findElement(By.xpath("//*[#id="blocks-left"]/div/div[3]/div[2]")).click();

Maybe come at it from a different angle. Do you really need to have the link clicked or do you have some Javascript function that gets called on click of the link (like window.loadMore). Can you call the function directly? Selenium is a bit annoying in the sense you can only click a visible element (I don't mean it has to be in the viewport - it just can't have a style like display:none;).

Related

Not able to click on Login button using selenium webdriver and Java

Hi I am trying to automate https://www.nextgenerationautomation.com and unable to click on login / SignUp button using Selenium 4
Steps:
Navigate to URL: https://www.nextgenerationautomation.com
Click on LogIn/SignUp button.
Issue: when I am using Thread.Sleep, code is working fine but when I am using explicit wait & implicit wait it's not working.
I have added Implicit in my base class at the time of driver initialization.
Here is the code that I have tried.
public class nextGenAutomationLoginPage extends Base {
#FindBy(xpath = "(//button[#class='_1YW_5'])[1]")
WebElement login;
public nextGenAutomationLoginPage() {
super();
PageFactory.initElements(driver, this);
// TODO Auto-generated constructor stub
}
public void clickOnLogin() throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
System.out.println(driver.getTitle());
// Thread.sleep(2000);
wait.until(ExpectedConditions.elementToBeClickable(login));
//wait.until(ExpectedConditions.presenceOfNestedElementLocatedBy(login, By.xpath("//div[#class='_10b1I']") ));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
js.executeScript("arguments[0].scrollIntoView();", login);
login.click();
//driver.findElement(By.xpath("(//button[#class='_1YW_5'])[1]")).click();
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[#id='comp-kaoxkn4a1']")));
String sign = driver.findElement(By.xpath("//div[#id='comp-kaoxkn4a1']/h1/span")).getText();
System.out.println(sign);
}
Note: I tried to add Scroll as well to add some wait before clicking.
DOM:
Please let me know if i am missing anything here.
Hi I got the point whats happening here :
Manually navigate to URL: https://www.nextgenerationautomation.com (Selenium also loading this URL)
Manually Immediately keep clicking on "LogIn/SignUp button" (Selenium also able to click therefore NO error in console )
"LogIn/SignUp" page not opening unless enter image description here (LinkedIn following count widget ) gets appear on screen
Once enter image description here gets appear manually click on "LogIn/SignUp button", now Login page is opening (Selenium able to click after Thread.Sleep)
Summery:
This is a codding defect on that page.
"LogIn/SignUp" is not clickable until enter image description here gets added on page
Your selenium code is perfectly fine :)
Xpath I have used is //span[text()='Log In / SignUp']
Thanks KS
To click() on the element with text as Log In / SignUp you can use either of the following Locator Strategies:
xpath:
driver.findElement(By.xpath("//div[starts-with(#id, 'defaultAvatar')]//following::span[text()='Log In / SignUp']")).click();
However, as the element is a dynamic element, so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[starts-with(#id, 'defaultAvatar')]//following::span[text()='Log In / SignUp']"))).click();
Actually your code is clicking the login button before the page is loaded properly. please add visibility condition.
wait.until(ExpectedConditions.visibilityOfElementLocated(by));

Can not click on a button; - "element is not interactable"

I have a script where I'm trying to push a button on an ecommerce site but when I run a script I get "Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable " .
When I put xpath into the chropath, element is located so I guess path driver.findElement(By.xpath("//form/fieldset[2]/button")).click(); is right. Please see attached screenshots and be gentle I'm new to programming and this site :/
I see, there's an Accept cookies button, if you want to click on that, you can use the below code :-
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#onetrust-accept-btn-handler"))).click();
and then click on Sleeve length like this :
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//form/fieldset[2]/button"))).click();
Note that you should open browser in full screen before interacting any web element.
driver.manage().window().maximize();
driver.get("Your URL");
You could use a JavascriptExecutor to trigger the click:
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", yourWebElement);
But I would recommand also to use explicit/implicit waits instread of Thread.sleep (I've linked an article, you can find more information on google).

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.

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.

Webdriver not finding xpath element

I'm trying to write some Selenium tests to test Pandora FMS using the Java implementation of the webdriver exported by the Selenium IDE.
The initial login part works just fine:
driver = new FirefoxDriver();
baseUrl = "http://brmew.lab.brmew.es";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(baseUrl + "/pandora_console/index.php");
driver.findElement(By.id("nick")).clear();
driver.findElement(By.id("nick")).sendKeys("my");
driver.findElement(By.id("pass")).clear();
driver.findElement(By.id("pass")).sendKeys("credentials");
driver.findElement(By.id("submit-login_button")).click();
Then, the problematic part, which is clicking a menu. I've tried to do the most simple approach:
driver.findElement(By.xpath("//ul[#id='subViews']/li[4]/a/div")).click();
But it did not work, so I tried:
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[#id='subViews']")));
myDynamicElement.click();
You can find the HTML I'm testing this with in this link (it's way too large to paste it here)
Hidden menu
Shown menu
Are you getting any exception? It seems that the element submemu item you are trying to click is invisible. If it get displayed on main menu click or mouse over you need to do that before perform click action to the element. For Example:
//Click main menu to open submenu
driver.findElement(By.xpath(".//*[#id='Views']/div")).click();
//now access submenu
driver.findElement(By.xpath(".//ul[#id='subViews']/li[4]/a")).click();
Or alternately more preferable way is:
WebElement viewsMenu = driver.findElement(By.xpath(".//*[#id='Views']/div"));
viewsMenu.click();
//or mouse over
Actions action = new Actions(webdriver);
action.moveToElement(viewsMenu).build().perform();
//now access submenu
viewsMenu.findElement(By.xpath(".//ul[#id='subViews']/li[4]/a")).click();
From ur given link, i can't find the monitoring > views > agent detail
But it seems that first u have to click on
monitoring and than wait
than click on
views and wait
than click on ur
agent detail
for mouse hover use the below code
//get element as ur wish by css or xpath or id
WebElement elem = driver.findElement(By.cssSelector("ur locator"));
Actions builder = new Actions(driver);
builder.moveToElement(elem).perform();
makeWait(5);
than after showing the element, than again get next element and use the code for hover.

Categories