I need to click on button on IE and I have used both the cases but none worked for me
driver.findElement(By.xpath("//button[text()='Existing Customer']")).click();
or
driver.findElement(By.xpath("//*[contains(text(), 'Existing Customer')]")).click();
or
WebElement obj = driver.findElement(By.xpath("//button[text()='Existing Customer']")).click();
Actions act = new Actions(driver);
act.moveToElement(obj).build().perform();
HTML Image
In place of "button" tag you can use * symbol, it can represent any tag.
driver.findElement(By.xpath("//*[text()='Existing Customer']")).click();
[or]
WebElement obj = driver.findElement(By.xpath("//button[text()='Existing Customer']"));
Actions act = new Actions(driver);
act.doubleClick(obj).build().perform();
Related
this is the example from our application
I am trying this code but not working
WebElement ele=driver.findElement(By.xpath("//a[#title='Menu for Distribution']"));
Actions act=new Actions(driver);
act.moveToElement(ele).perform();
Thread.sleep(3000);
for (int i=3; i<=10; i++)
{
System.out.println(i);
ele1=driver.findElement(By.xpath("//a[#target='frame_main'])[i]"));
ele1.click();
}
Please use the following method to click submenu option,
by calling clickSubMenu("New Consignee");
public void clickSubMenu(String optionName){
By mainMenu = By.xpath("//a[#title='Menu for Distribution']");
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(mainMenu));
action.build().perform();
By subMenu = By.xpath("//a[text()='"+optionName+"']");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(subMenu));
driver.findElement(subMenu).click();
}
Steps :
Hover over to the main menu
wait for the visiblity
Click the submenu
I'm having troubles regarding webdriver not being able to click checkboxes sometime and just skipping them, both in Firefox and Chrome.
I've tried different solutions such as
click();
action.moveToElement(checkbox).clickAndHold(checkbox).release().perform();
jse.javascriptExecutor(argument[0].click(),checkbox).
Here I provide the Javascript code I have for the click event
...
var selectCorrectOption = function () {
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
$(this).find('> input').prop('checked', false);
} else {
$(this).addClass('selected');
$(this).find('> input').prop('checked', true);
}
};
$('.option > .input-container').on('click', selectCorrectOption);
...
the HTML code where it is attached the javascript click event
<div class="input-container selected" data-choice-id="2">
<input type="radio">
</div>
the Java code data uses a data-attribute to access the element is question. Notice also that once the div is clicked, a 'selected' class appears(which is the current state) on the code below.
WebDriverWait wait = new WebDriverWait(driver, 20);
JavascriptExecutor jse = (JavascriptExecutor)driver;
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[data-choice-id='"+ wrongOptionVal +"']")));
radio=wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("[data-choice-id='"+ wrongOptionVal +"']")));
jse.executeScript("arguments[0].scrollIntoView()", radio);
jse.executeScript("arguments[0].click()", radio);
I expected it to be consistent ,most of the times work, but there are always that one or two times it fails.
I do not know why this happens, I do however know how this can be addressed. You can implement a FluentWait with polling mechanism, which will do three things:
locate the element,
select the radio box,
return getAttribute("class").contains("selected") value.
If getAttribute("class").contains("selected") will result in false the process should repeat.
FluentWait<WebDriver> fluentWait = new FluentWait<>(driver)
.pollingEvery(Duration.ofMillis(300))
.withTimeout(Duration.ofSeconds(10));
fluentWait.until(new Function<WebDriver, Boolean>() {
#Override
public Boolean apply(WebDriver driver) {
WebElement element = driver.findElement(By.cssSelector(radioCssSelector));
element.click();
return element.getAttribute("class").contains("selected");
}
});
I am trying to click the link under the navigation bar. I tried driver.findelement in this code snippet. It selects the link but the click event is not taking place.
WebElement menu=driver.findElement(By.xpath(".//*[#id='bs-example-navbar-collapse-1']"));
//WebElement menu = driver.findElement(By.XPATH("Coplete_navigationbar_xpath")); List<WebElement>
List<WebElement> allLinks = menu.findElements(By.tagName("a"));
String MenuOptn="";
for (WebElement w : allLinks)
{
MenuOptn=w.getText();
if(MenuOptn.equalsIgnoreCase("TRACKING"))
{
// System.out.println("tracking");
w.click();
System.out.println("tracking");
break;
}
System.out.print(w.getText());
}
Try click using javascript
WebElement element = webDriver.findElement(locator);
JavascriptExecutor executor = (JavascriptExecutor) webDriver;
executor.executeScript("arguments[0].click();", element);
Try below options:
driver.FindElement(By.Xpath("//a[contains(., '<link_text>')]")).click();
or
new Actions(driver).moveToElement(driver.FindElement(By.Xpath("//a[contains(., '<link_text>')]")),10,10).doubleClick().perform();
I hope it will be helpful
I have a query on mouseover action in Selenium Webdriver Java.
Consider i have a background image with id "Lfade" with opacity 0.5. If i hover the mouse then a button would be shown.
I want to click on the button to take me to another screen. How do i do this ???
I have tried this, but it does not work
Actions builder = new Actions(driver);
WebElement tagElement = driver.findElement(By.id("Lfade"));
builder.moveToElement(tagElement).build().perform();
Html
div id="homeslant"
div id="wrapper"
div id="lFade" class="learn" style="opacity: 0.5; visibility: visible;"
Button
div class="descbtn"
a class="btn dwmbutton" href="/learn/index.html">KNOW MORE</a>
Not sure about what you are trying to do but I notice an error here:
builder.moveToElement(tagElement).build().perform();
Actually, the .perform() includes a .build().
May be you were referring to .click() so
builder.moveToElement(tagElement).click().perform()
As a pattern for your objective I would:
find the first image
move to the image
wait
find the button
click the button
I used Robot class for mouse hover. Please try the below code.
Robot robot = new Robot();
Point MyPoint = tagElement.getLocation();
robot.mouseMove(MyPoint.getX(), MyPoint.getY());
After that use the normal selenium click to click on the button.
You need to move to the button and perform a click.
Move to the image
wait
Move to the button and click
Actions act = new Actions(driver);
WebElement tagElement = driver.findElement(By.id("Lfade"));
act.moveToElement(tagElement).click().build().perform();
WebElement _button= driver.findElement(By...);
act.moveToElement(_button).click().build().perform();
Try this
#FindBy(xpath="//*[#id='chromemenu']/span/a") WebElement menuHoverLink;
#FindBy(xpath="//*[#id='dropmenu1']/a[1]") WebElement subLink;
Actions maction = new Actions(driver);
maction.moveToElement(menuHoverLink).build().perform();
Thread.sleep(2000);
subLink.click();
Use this code it will work :
Actions builder = new Actions(driver);
WebElement tagElement = driver.findElement(By.id("Lfade"));
WebElement buttonElement = driver.findElement(By.classname("btn"));
builder.moveToElement(tagElement).moveToElement(buttonElement).click().perform();
Try this
public void HoverAndClickObject(WebDriver driver, String property1, String property2, String path) throws SAXException, IOException, ParserConfigurationException
{
//get object properties from the xml file repository
Actions action = new Actions(driver);
String[] element1 = xmlParser(path, property1);
String[] element2 = xmlParser(path, property2);
switch (element1[0].toUpperCase())
{
case "XPATH":
driver.findElement(By.xpath(element1[1])).click();
action.moveToElement(driver.findElement(By.xpath(element2[1]))).click().build().perform();
break;
case "ID":
driver.findElement(By.id(element1[1])).click();
break;
case "NAME":
driver.findElement(By.name(element1[1])).click();
break;
case "LINKTEXT":
driver.findElement(By.linkText(element1[1])).click();
break;
case "CSSSELECTOR":
driver.findElement(By.cssSelector(element1[1])).click();
break;
}
}
I'm writing a Windows Phone Mango app. I have a hyperlink that contains email addresses. I want it to show a message box on tap:
Hyperlink href = new Hyperlink();
href.Click += (s, r) =>
{
MessageBox.Show("href clicked");
};
// add href to RichTextBox
When I click on the hyperlink in the emulator, nothing happens. The click += line is hit in a debugger, but the new EmailComposeTask() line isn't.
What am I doing wrong? Is Click not the event I want?
Update: I switched EmailComposeTask to MessageBox to verify that this issue isn't related to the email compose task not running on the emulator. I've reproduced this issue on my device.
The following code works:
GestureService.GetGestureListener(href); // adding this makes it work
href.Click += (s, r) =>
{
MessageBox.Show("href clicked");
new EmailComposeTask() { To = entireLink.Value }.Show();
};
href.Inlines.Add(new Run() { Text = entireLink.Value });
GetGestureListener creates a gesture listener if it doesn't already exist.
You should use HyperlinkButton Class, Hyperlink is a class used with rich text document rendering.
Following code is runs on device. text is RichTextBox object.
var linkText = new Run() { Text = "Link text" };
var link = new Hyperlink();
link.Inlines.Add(linkText);
link.Click += (o, e) =>
{
MessageBox.Show("Link clicked");
};
var paragraph = new Paragraph();
paragraph.Inlines.Add(link);
text.Blocks.Add(paragraph);