I'm new to selenium. I was trying to select radio button from a form. It has id. By.id("test12")).getAttribute("value")), displays the correct value,but if i do By.id("test12")).click(); did not click the element.
driver.findElement(By.xpath("//*#id='test13']")).getAttribute("Value"));
also displays the name. but click() didn't work.
I got org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 30.09 seconds
<input id="test12" class="with-gap" type="radio" value="P" name="group1">
<input id="test13" class="with-gap" type="radio" value="C" name="group1">
List<WebElement> eRB=driver.findElements(By.name("group1"));
System.out.println(eRB.size());
for(int i=0;i<eRB.size();i++)
{
System.out.println(eRB.get(i).getAttribute("id")+" is Displayed = "+eRB.get(i).isDisplayed());
}
the o/p was:
4
test13 false
test12 false
test13 false
test12 false
Could anyone tell what i'm doing wrong? Thanks.
#Suba Narayanan, use webdriver wait to check the visibility of the element before clicking.
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.id("test12"))).click();
Try the below code for clicking the radio button:
driver.get("https://www.jobsforher.com/employer/account/sign_up");
Thread.sleep(5000L);
driver.findElement(By.xpath("//*[#id='test2']/div/div/div[1]/div[1]/div[2]/div/label")).click();
Thread.sleep(5000L);
You can customise your wait by waiting for specific element.
Let me know for more queries.
Happy Learning. :)
Related
I have a page as this,
And I want to click the top checkbox, which by default, would automatically select all the checkboxes in that list. As you can see, I have created the xpath value, and it matches the checkbox area that I want to pick. But when I was to have it executed in selenium, the checkbox checking never happens.
xpath_var = "//th[#class='selection-cell-header']/div[1]/input[1]"
WebDriverWait(self.driver, 30).until(
EC.element_to_be_clickable((By.XPATH, xpath_var,))).click()
And here is the exception I have received:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <input class="ftnt-checkbox" readonly="" type="checkbox" id="" value=""> is not clickable at point (305, 162).
Other element would receive the click: <label for="ftnt-checkbox"></label>
Any idea what could be wrong ? work around for such case? Manual clicking was ok, in that case.
Thanks,
Chun
The exception message indicates that there is another element that will receive the click. This is a kind of protection in Selenium.
You can try to click this label that is on top of your element. Or click the parent div. Or click your element with JavaScript.
WebElement element = driver.findElement(By.id("gbqfd"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Reference:
How to click an element in Selenium WebDriver using JavaScript?
I checked the other posts like this but they weren't able to help me. I'm just starting out with Selenium and I'm having trouble clicking a radio button.
This is what is in the inspector.
input id="createCreds" name="addUser" aria-required="true" ng-model="formData.newUserType" ng-required="addNewUser.$submitted && !formData.newUserType" "="" class="ng-valid ng-valid-required ng-dirty ng-valid-parse ng-touched" aria-checked="true" aria-invalid="false" value="createCreds" type="radio"
Here is what I've tried
Trial 1 :
WebElement userRadioBtn = driver.findElement(By.id("createCreds"));
userRadioBtn.click();
Trial 2 :
driver.findElement(By.xpath("//*[#id="createCreds"]']")).click();
Trial 3 :
driver.findElement(By.id("createCreds")).click();
Trial 4 :
input[#value='createCreds']/following-sibling::label
I was trying to copy this guy around 6 minutes.
https://www.youtube.com/watch?v=BKbzW4S2qQ0
I was hoping to find something like Click RadioButton css=#createCreds or xpath=//*[#id="createCreds"]
As per the HTML you have shared to click on the Radio Button as the element is an Angular element you have to induce WebDriverWait and you can use either of the following options as follows :
cssSelector :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.ng-valid.ng-valid-required.ng-dirty.ng-valid-parse.ng-touched#createCreds"))).click();
xpath :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='ng-valid ng-valid-required ng-dirty ng-valid-parse ng-touched' and #id='createCreds']"))).click();
I am trying to click a button:
<button type="button" class="yt-ui-menu-item yt-uix-menu-close-on-select vm-playlist-monetize-videos">
<span class="yt-ui-menu-item-label">Monetize</span>
</button>
I have tried the following:
//*[#id=\"aria-menu-id-7\"]/ul/li[1]/button/span
Which won't work because the id is sometimes different than 7
I have also trued
//span[.='Monetize']
Which also doesn't work. How can I go about getting the xpath to click the above button?
You can try starts-with function if the id property is not unique. Try following xpath:
//*[starts-with(#id, 'aria-menu-id')]/descendant::button/span[text()='Monetize']
Let me know, if it works for you.
Try below code, using explicit wait method along with xpath.
WebDriverWait wait = new WebDriverWait(driver, 10); //wait for 10 seconds to find the web-elment.
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//button/span[text()='Monetize']"))));
WebElement button = driver.findElement(By.xpath("//button/span[text()='Monetize']"));
Click the button not the span element
//*[text()='Monetize'][#class='yt-ui-menu-item-label']/..
(or use the below format)
//*[contains(text(),'Monetize')][#class='yt-ui-menu-item-label']/..
I have an HTML code as follows:
<label class="top" for="id_gender2">
<div id="uniform-id_gender2" class="radio">
<span>
<input id="id_gender2" type="radio" value="2" name="id_gender">
</span>
</div>
Mrs.
</label>
The radio button is getting selected after mouse hover.
I have tried with all possible attributes for selection but I am getting element not found exception.Please let me know how to write java script in webdriver.
Try following code and let me know the result:
Actions action = new Actions(driver);
WebElement hover = driver.findElement(By.xpath("//*[#class='radio hover']"));
action.moveToElement(hover).moveToElement(driver.findElement(By.xpath("//input[#id='id_gender2']"))).click().build().perform();
UPDATE
Actually there is no need in mouse hover. Target radio seem to be initially non-clickable, so you just need extra time to wait for page complete rendering:
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#id_gender2")));
driver.findElement(By.cssSelector("input#id_gender2")).click();
Try javascript executor as follow:
WebElement element = driver.findElement(By.id("id_gender2"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
Your element is probably not loaded properly while getting error. You'd better use explicit wait in your script. Wait until your specific element loaded to be visible/clickable. I think, this may help you.
Code snippet:
By yourElementToSelect = By.id("id_gender2");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(yourElementToSelect));
driver.findElement(yourElementToSelect).click();
I would like to click on a "New Contact" button for my selenium script. I have tried:
driver.findElement(By.id("btn-group.contact_list-menu-contact_add")).click();
And by xpath as well, but it is not working. How could I get this working?
<div class="btn-group left">
<a id="contact_list-menu-contact_add" class="Button btn-contactadd primary SaveItem" href="javascript:">New Contact</a>
</div>
You are searching by an incorrect id value, use contact_list-menu-contact_add instead:
driver.findElement(By.id("contact_list-menu-contact_add")).click();
Or, by a CSS selector:
driver.findElement(By.cssSelector(".btn-group .btn-contactadd")).click();
driver.findElement(By.cssSelector(".btn-group #contact_list-menu-contact_add")).click();
driver.findElement(By.cssSelector("#contact_list-menu-contact_add")).click();
Or, by a link text:
driver.findElement(By.linkText("New Contact")).click();
If the target element is inside an iframe, you would need to switch into the context of the frame before searching for the element. Assuming that your frame has contactURL id, this is how to switch to it:
driver.switchTo().frame("contactURL");
If you are getting NoSuchElementException as you have mentioned in the comment, There are may be two reason :-
May be when you are going to find element, it would not be present on the DOM, So you should implement WebDriverWait to wait until element visible and clickable as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.id("contact_list-menu-contact_add")));
el.click();
May be this element is inside any frame or iframe. If it is, you need to switch that frame or iframe before finding the element as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("contactURL"));
WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.id("contact_list-menu-contact_add")));
el.click();
Edited :- As I see from your provided HTML this button is inside <div id="btn-new-group" class="btn-group-actions left" style="display: none;"> which is set to be invisible, that's why you are not able to find button. You should make it visible first then try to find as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("contactURL"));
WebElement invisibleDiv = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("btn-new-group")));
//Now make it visible first
((JavascriptExecutor)driver).executeScript("arguments[0].style.display = 'block';", invisibleDiv);
//Now find contact button
WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.id("contact_list-menu-contact_add")));
el.click();
Hope it helps...:)