So I ran into a new issue today that I have not experienced for, and it relates to the nature of the Chrome Driver (I believe Chrome is the only one that does this..). I am aware that when you click an element using .click() it clicks in the center. However this is troubling because I am trying to click a checkbox that just so happens to have a link nested in the center.
I have tried using the JavaScript Executor as well and no luck.. Does anyone know a way around this? Yes I have tried just accessing the box but it doesnt have an identifier I can use..
You can click using coordinates
Coordinates co = element.getCoordinates();
This issue could be resolved by two methods
Method 1: Find correct xpath of checkboxx use default click
driver.findElement(By.xpath("CheckBoxXPath").click();
Method 2: If you really want to click on centre of WebElement then you Actions class method click(WebElement target) this method click on mid of WebElement.
Refer How to use Actions class clcik method
Since you didn't add here the check box html I will assume it is something like:
<input type="checkbox" id="checkbox_id">
<label for="checkbox_id">Something</label>
So you just have to click the input and not the label. It will look like this:
driver.findElemnt(By.id("checkbox_id")).click();
Or using xPath:
driver.findElemnt(By.xpath("//input[#type='checkbox']")).click();
Related
I have an html radiobox with Yes or Not choice.
But the Yes and Not class have the same class name.
CODE FOR YES
<div class="quantumWizTogglePaperradioRadioContainer"><div class="quantumWizTogglePaperradioOffRadio exportOuterCircle"><div class="quantumWizTogglePaperradioOnRadio exportInnerCircle"></div></div></div>
CODE FOR NOT
<div class="quantumWizTogglePaperradioRadioContainer"><div class="quantumWizTogglePaperradioOffRadio exportOuterCircle"><div class="quantumWizTogglePaperradioOnRadio exportInnerCircle"></div></div></div>
When I use this code, the program click on the first one in the radiobox form.
radioButton = String.format("//div[#class='quantumWizTogglePaperradioOffRadio exportOuterCircle']");
driver.findElement(By.xpath(radioButton)).click();
how can I recognize them?
If there are only 2 radiobox, use this in the order
First radiobox :
radioButton = String.format("(//div[#class='quantumWizTogglePaperradioOffRadio exportOuterCircle'])[1]");
Second radiobox :
radioButton = String.format("(//div[#class='quantumWizTogglePaperradioOffRadio exportOuterCircle'])[2]");
I will suggest using absolute XPath rather than relative XPath it might resolve it. please see below how absolute XPath is different from relative using the google search bar.
Relative XPath - `//*[#id="tsf"]/div[2]/div/div[1]/div/div[1]/input`
absolute XPath - `/html/body/div/div[3]/form/div[2]/div/div[1]/div/div[1]/input`
or you can use the reference of other elements, For example, suppose if you have a div before with yes and no option with id="Radiobutton". you can still recognize yes and no as below as
yes - //*[#id="Radiobutton"]/div[1]
No - //*[#id="Radiobutton"]/div[2]
Let me know if you need any clarification
You can see above the radio button I can give you an example with that. so we are trying to get possible Paths for radio buttons in div highlighted in black. similar to your problem it has the same class name. so below are the some of possible ways to do it
relative XPath Using Form id which is up in the hierarchy
Yes - `//*[#id="create-form"]/div[3]/div/div/div/ul/li/div[2]/div/div/div[1]`
No - `//*[#id="create-form"]/div[3]/div/div/div/ul/li/div[2]/div/div/div[2]`
Using Absolute path - the absolute path is tracking hierarchy from top to the element. In this case it will be
Yes - `/html/body/div/section/div/div/div/div/form/div[3]/div/div/div/ul/li/div[2]/div/div/div[1]`
No - `/html/body/div/section/div/div/div/div/form/div[3]/div/div/div/ul/li/div[2]/div/div/div[2]`
If you see the difference here between 1 and 2. In absolute path instead of starting from form id as a reference, it starts right from HTML tag and tracks the element in DOM tree.
I want to send a text to a div element in web.whatsapp. I tried several ways ie using XPath, cssSelector, class name etc. but none is working for me.
This textbox comes when we attach an image in web.whatsapp.
Please step into the div and use the div with the class "selectable-text". As this is a contenteditable div you should be able to click into it and call then sendKeys() onto the element.
if this does not help please let me know then I will look in our code how we are filling such divs exactly.
You need to drill down further until you get to a web element that takes an input.
I try to click in a button like imagen.
not working using class or xpath
this is the button
this is the inspect from this button.
this is the code trying to click on the button:
driver.findElement(By.xpath("/html/body/div[10]/button")).click();
this is the xpath from the before:
driver.findElement(By.xpath("/html/body/div[10]"));
please could someone help me!!!
Every simple change in the page will cause your code to stop functioning, try to always make use of class or id and navigate to it's siblings/parent nodes.
You can do that in 2 ways
1.By using CssSelector (Right click on the Element in DevTools -> Copy -> Copy Selector)
driver.FindElement(By.CssSelector("CopiedText")).Click()
2.By using XPath and accessing it through it's parent (Example for your case)
driver.FindElement(By.xpath("//div[#class='advertising-layer']/button")).Click()
I experienced the similar issue while accessing a button in a dialog box. I tried with XPath, id, it didn't work but it worked with CSS selector.
By using CSS selector I made the selenium webdriver to click on a button, pick a value from the drop-down in the dialog box, type a value in the text box.
I am not sure why exactly it worked with CSS selector and not with XPath. I'd be grateful if someone has a description for this.
If you want to get CSS selector of a particular element follow the below steps
Open the application in Firefox
open Inspect element, and in the inspector ( the place where you see the HTML code)
Select the element in the page to view the particular code for the inspector by using this button
right click on the highlighted part of the code and hover on copy, you will find CSS selector
Let me know if this works out for you.
As per the HTML you have shared to invoke click() on the desired element you have to induce WebDriverWait and you can use the following solution:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("/div[#class='advertising-mask']//button"))).click();
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);
I am building a test suite for a basic registration page (it has a large number of text fields + some radio buttons and checkboxes here and there). I am using Selenium Webdriver solution and tests are written in Java. While the tests are running fine on Firefox and Chrome, the Internet Explorer tends to run into trouble when it comes to clicking on radio buttons or checkboxes. All the radio buttons and checkboxes have id-s defined and from what I've learned it's the most convenient way to find an element on the page, so I was quite surprised when I started getting these issues. The method for finding the radio button looks like this:
public static WebElement rad_Male(WebDriver driver) {
element = driver.findElement(By.id("male"));
return element;
}
The click is done in a following way:
rad_Male(driver).click();
As I said, Firefox and Chrome can easily click on checkboxes and radio buttons, but when running tests in IE I get a following exception (the element is visible all the time and I can click on it with mouse):
org.openqa.selenium.ElementNotVisibleException: Cannot click on element
I've also tried using an explicit wait in order to let the elements load before accessing them, but had no luck - I get TimeoutException as soon as the function times out. I suspect it has something to do with the page design, but unfortunately I have no access to the page source code, so I cannot change the page structure to make it easier to test.
The radio button is placed inside a number other divs and I think there is also a table used to align this and other elements around, but this doesn't look too complicated. Here's the code for radio button:
<input type="radio" value="M" name="sex" id="male" tabindex="110">
I think I saw some javascript click suggestion in one of the similar topics, but before resorting to this I wanted to make sure that there is no other way to make it work using the means that Webdriver provides.
I've just started learning Selenium and I try to get my work done on the go while learning new stuff all the time, so I am not too experienced with this yet.
If you would like some more details, please ask as I am not sure if I've got all included.
Thanks in advance!
Hi again and thanks to everyone who responded! A friend of mine had a look at this issue and managed to figure out what was causing this. The radio button was actually contained inside another div in a following way:
<div class="radio" id="uniform-male">
<span>
<input type="radio" value="M" name="sex" id="male" tabindex="110">
</span>
</div>
It appears that this parent div "uniform-male" kind of concealed this button, because Selenium was able to click on this div and as a result, the radio button underneath it was clicked.
I guess I should have posted the code for the radio button along with some code of it's parent elements in the first hand, so it would have been easier to debug it.
Once again I appreciate all the help I received from you on this question, thanks!
try using this before you click on the element, maybe IE is a bit slower:
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(anId)));
As per the error you are getting, I think selenium is trying to click on the element, i.e., radio button, which is probably not visible yet.
To resolve this, try adding an explicit wait in the method rad_Male like this (Assuming 'element' is a reference of 'WebElement class'):
public static WebElement rad_Male(WebDriver driver) {
//waiting 30 seconds for the element to be visible
element = new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.id("male")));
return element;
}
Then, use it to click the button like this:
element = rad_Male(driver); //Fetching the value returned by rad_Male method
if(element!= null)
element.click();
else
System.out.println("Element is not visible");