Selenium Webdriver, clicking a button not working - java

I am having two issues in clicking two buttons during my automation
First : A View button which have the following Details
<button class="veiw-btn" data-toggle="collapse" data-target=".toggle-content1" aria-expanded="false" aria-controls="toggle-content1" ng-click="gotoAnchor(flightResult.FlightId)">VIEW</button>
There are several VIEW buttons on the page but they are differentiated with the toggle-content (they have numbers 1,2,3,4) I just need to select the first one and click on it
Second :
After clicking the View i want to also click the Continue Button with the following code
<div class="text-center">
<button class="flight-book-btn" type="button" ng-click="select(false, flightResult);">
<span>Continue</span>
</button>
</div>
My Major issue is the First Code but if i can get help with both i will be glad . I have not been able to click on the First VIEW Button
i have tried some samples online but they have not worked for me
I expect to be able to click the VIEW and Continue Buttons
CODE:
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
log.debug("Fastest Sort Available ");
log.debug("Now about to click VIEW Airline Details ");
// driver.findElement(By.xpath("//button[text()='VIEW' and #data-target='.toggle-content1' and #aria-controls='toggle-content1']")).click();;
driver.findElement(By.cssSelector("button[class=\"veiw-btn\"][data-target='.toggle-content1'']")).click();

Try using the css selector or Xpath
CSS:
driver.FindElement(By.CssSelector("button[class="veiw-btn"][data-target='.toggle-content1']").Click();
xpath:
driver.FindElement(By.XPath("//button[#class='veiw-btn'][#data-target='.toggle-content1']").Click();

You can use this xpath to click on VIEW button :
//button[text()='VIEW' and #data-target='.toggle-content1' and #aria-controls='toggle-content1']
For clicking on Continue button you can try with this xpath :
//span[text()='Continue']/parent::button[#class='flight-book-btn']

I have fixed this by using this
WebElement element= driver.findElement(By.xpath("//button[text()='VIEW' and #data-target='.toggle-content1' and #aria-controls='toggle-content1']"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);

Related

How to click on the psuedo element that has a pop up checkbox element in selenium?

I am trying to automate test script and ran upon the pseudo element which seems to not clickable using normal element finders. The element to be clicked is a field after which the checkbox pop which is to be clicked again.
<span class="mat-select-placeholder mat-select-min-line ng-tns-c156-64 ng-star-inserted">
:before==$0
</span>
enter image description here
I have tried to click it using Actions method and JavascriptExecutor but was not able to. Please help.
This is the codes that I have tried.
driver.findElement(By.xpath("//span[#class='mat-select-placeholder mat-select-min-line ng-tns-c211-60 ng-star-inserted']").click();

Unable to locate and click checkbox ::before using Selenium Webdriver

I'm trying to click a checkbox but it's keep clicking the link 'terms and conditions'. Although my xpath (mentioned below) work on a minimized window but it's failing to click the checkbox when the window is maximized because the href (image) appears in the second line next to checkbox. Looking for some suggestions on clicking the checkbox widget on maximized window. I need to get a focus on it.
Interestingly, when i hover over the ::before (css selector) only than the widget gets highlighted.
<div class="checkbox u-mar-bot-5">
<div class="checkbox__container">
<input class="checkbox__input" type="checkbox" id="basket-contact-terms" required data-parsley-multiple="basket-contact-terms" style>
<label class="checkbox__label checkbox__label--has-link checkbox__label--small" for="basket-contact-terms" style>
::before
"I have read and agree to " <a class="text-link text-link--base text-link- small" href="/terms-conditions" target="_blank">Terms and Conditions</a>
</label>
</div>
</div>
image: Terms and Conditions
I tried a few options that keep failing to check the box and instead the link 'terms and conditions' gets the click. I must be missing something basic.
driver.findElement(By.xpath("//label[#for='basket-contact-terms']")).click();
driver.findElement(By.xpath("//label[contains(#class,'checkbox__label checkbox__label--has-link checkbox__label--small')]")).click();
I did looked around and found someone suggested to use this (below) so i tried but didn't work:
WebElement elem = driver.findElement(By.xpath("//div[contains(#class,'checkbox u-mar-bot-5')]"));
Actions action = new Actions(driver);
action.moveToElement(elem).click().build().perform();
Any suggestion would be appreciated.
Since you tried the ID of the INPUT and it threw an error that it wasn't visible, I would first try a wait to see if it will become visible. (I'm assuming it won't but it's worth a try first).
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("basket-contact-terms"))).click();
If that doesn't work, I would next try to click a different position on the element. By default, Selenium clicks on the center of the element. In your case, I think this is what's causing the issue. You can use Actions to click the upper left (1,1) of the element.
WebElement label = driver.findElement(By.xpath("//label[#for='basket-contact-terms']"));
new Actions(driver).moveToElement(label, 1, 1).click().perform();
You can try with
WebElement elem = driver.findElement(By.id("basket-contact-terms"))

How to find XPath of select file button?

I need to click on Select file button. Have tried by using following XPath:
driver.findElement(By.xpath("//div[#class='ant-modal-body']//button[contains(#class,'ant-btn-ghost')]/i")).click();
But the above code didn't work for me.
Following is the code which I get after pressing F12:
<button class="ant-btn ant-btn-ghost" type="button">
<i class="anticon anticon-upload"/>
<span> Select File </span>
</button>
As you mentioned in your comments that you solved your issue through Thread.sleep(2000) that would be against all the Best Practices. To to click on the button with text as Select file you have to induce WebDriverWait and you can use the following line of code :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='ant-modal-body']//button[#class='ant-btn ant-btn-ghost']//span[contains(.,'Select File')]"))).click();
First try with :
driver.findElement(By.xpath("//span[contains(text(), 'SelectFile')]")).click();
If it doesn't work try :
WebElement composeBtn = driver.findElement(By.className("ant-btn ant-btn-ghost"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", composeBtn);

Selenium - webDriver not stable-error stack trace

I am not able to click on the submit button inside the modal. It only works sometimes - it's not stable.
Below is my HTML element:
<button id="submit-btn" name="submit" data-dismiss="modal" type="submit" class="btn btn-info btn-sm submit projectSaveBtn">Submit</button>
Here I'm using id for locating the element, but I'm not able to click on the submit button inside the modal.
My Java code:
WebElement element1 = driver.findElement(By.id("submit-btn"));
Actions actions = new Actions(driver);
actions.moveToElement(element1).build().perform();
wait.until(ExpectedConditions.elementToBeClickable(element1)).click();
When we use the action class, selenium takes mouse and keyboard controls. If we interact with mouse or keyboard at the time of test execution (specially the actions statement execution), it may fail some times.
You can try with actions class and don't interact until it completes the test execution. it may solve your issue.
Instead of using the id attribute you can use an xpath as follows :
WebElement element1 = driver.findElement(By.xpath("//button[#id='submit-btn']"));
element1.click();
or
WebElement element2 = driver.findElement(By.xpath("//button[#class='btn btn-info btn-sm submit projectSaveBtn' and #id='submit-btn']"));
element2.click();
If it's a windows modal dialog box, you might want to first switchTo() to the modal and then perform accept() on it.
driver.switchTo().alert().accept();
driver.switchTo().window("");
I solved the issue, it was timing issue .Modal was opened before the page loaded properly so giving the time before clicking/opening modal solves the issue .
Here is my code -->
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement add = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.btn.projectAddBtn")));
add.click();
This solved my issue.Thank you all for the support :-)

Cannot click on input type = "radio" that is inside a HTML label?

I have a problem where I cannot click on the input type = "radio" using the id="same-supplier-no":
<label for="same-supplier-no" ng-class="{checked: !ctrl.energyModel.BillViewModel.SameSupplier}" class="">
<input type="radio" id="same-supplier-no" name="same-supplier" ng-model="ctrl.energyModel.BillViewModel.SameSupplier" ng-value="false" ng-change="ctrl.sameSupplierValueChanged(); ctrl.analyticsProvider.sendVirtualPageView('SameSupplierNo')" class="ng-pristine ng-untouched ng-valid" value="false">
<span></span>No
</label>
In Selenium when I do:
driver.findElement(By.id("same-supplier-no")).click();
it says that "element not visible". There are previous radio buttons in the HTML that have labels on it and they have ID's for the labels (this one does not).
I just refer to the label id and click that instead and it works. But this one doesn't have an ID for the label and I want to avoid xpath usage, however, if I have no choice, then I will just select via xpath/css.
I have also tried via locator name i.e. By.className("same-supplier"); and using a For loop and if statement to store the input type=radio element, still doesn't work.
Question is: why can I not click the input type = "radio", why is in not visible to Selenium? Is it because it is nested inside a Label? If so, does anyone know how I can click it using the id or just getting to the element?
Thank you.
Note: I am using Java 7 for Selenium.
From the chat conversation I could try some stuff on the website you are using. The radio never becomes visible. As a result of this it is impossible to use the click() method on it.
The label that contains this radio is visible however. This means you could just use click() on this label.
driver.findElement(By.cssSelector("label[for='same-supplier-no']")).click();
Or if you really want to find the element using the radio you could use xpath.
driver.findElement(By.xpath("//*[#id='same-supplier-no']/parent::label")).click();
use action to click on the lable and after that use:
Actions actions = new Actions(driver);
IWebElement menuHoverLink = driver.findElement(By.XPath("//label[#for='same-supplier-no']"));
actions.moveToElement(menuHoverLink);
actions.click();
actions.perform();
driver.findElement(By.id("same-supplier-no")).click();
After you click on "find postcode" button, add below two line of code -
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath(".//*[#id='same-supplier-question']/div/div/label[2]/span"))));
This is has worked at my end...
You can change the xpath from absolute to relative....

Categories