I have HTML as below
<div class="summary-row">
<div class="text-center" id="summary-back">
<a href="/Health/Dependents">
<svg class="svg-inline--fa fa-chevron-left fa-w-8 font-32" data-auto="back-btn" aria-hidden="true" data-prefix="fal" data-icon="chevron-left" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 512" data-fa-i2svg=""><path fill="currentColor" d="M238.475 475.535l7.071-7.07c4.686-4.686 4.686-12.284 0-16.971L50.053 256 245.546 60.506c4.686-4.686 4.686-12.284 0-16.971l-7.071-7.07c-4.686-4.686-12.284-4.686-16.97 0L10.454 247.515c-4.686 4.686-4.686 12.284 0 16.971l211.051 211.05c4.686 4.686 12.284 4.686 16.97-.001z"></path></svg><!-- <i class="fal fa-chevron-left font-32" data-auto="back-btn"></i> -->
</a>
</div>
And RemoteWebDriver unable to find the element and I assume its due to the hidden attribute, How can click on this element ?
You can click on the element using JavaScriptExecutor like:
WebElement element = driver.findElement(By.id("summary-back"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
To click() on the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:
CssSelector:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("div.summary-row > div#summary-back > a[href=/Health/Dependents]"))).Click();
XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[#class='summary-row']/div[#id='summary-back']/a[#href='/Health/Dependents']"))).Click();
I just changed attribute "aria-hidden" from "true" to "false" and after that I clicked.
Perhaps that code can help you.
public void ClickElementHidden()
{
((IJavaScriptExecutor)driver).ExecuteScript("document.getElementsByClassName('svg-inline--fa fa-chevron-left fa-w-8 font-32')[0].setAttribute('aria-hidden', 'false')");
driver.FindElement(By.CssSelector("#summary-back > a > svg")).Click();
}
Related
I've tried lots of ways but none seem to work its not a clear id="ezidgrab" the element can be found on this url: https://www.ahem.email/mailbox/QI2R89LNDT but how do I click the email there with selenium?
What I have tried:
1.
driver.FindElement(By.XPath("//p[contains(text(), 'Thank you for registering your email')]")).Click();
2.
driver.FindElement(By.TagName("mat-list-item")).Click();
3.
driver.FindElement(By.ClassName("mat-list-item-content")).Click();
and the html of the link:
<app-email-info _ngcontent-ahem-c5="" _nghost-ahem-c9="">
<mat-list-item _ngcontent-ahem-c9=""
class="ahem-hand-pointer mat-list-item ahem-email-unread mat-3-line">
<div class="mat-list-item-content">
<div class="mat-list-item-ripple mat-ripple" mat-ripple=""></div>
<div class="mat-list-text">
<h2 _ngcontent-ahem-c9="" class="mat-line" mat-line="">Jagex</h2>
<p _ngcontent-ahem-c9=""
class="mat-line"
mat-line="">Thank you for registering your email</p>
<p _ngcontent-ahem-c9="" class="mat-line" mat-line="">9 minutes ago</p>
</div>
<fa-icon _ngcontent-ahem-c9="" class="ng-fa-icon">
<svg aria-hidden="true" focusable="false" data-prefix="far" data-icon="envelope" class="svg-inline--fa fa-envelope fa-w-16" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<path fill="currentColor" d="M464 64H48C21.49 64 0 85.49 0 112v288c0 26.51 21.49 48 48 48h416c26.51 0 48-21.49 48-48V112c0-26.51-21.49-48-48-48zm0 48v40.805c-22.422 18.259-58.168 46.651-134.587 106.49-16.841 13.247-50.201 45.072-73.413 44.701-23.208.375-56.579-31.459-73.413-44.701C106.18 199.465 70.425 171.067 48 152.805V112h416zM48 400V214.398c22.914 18.251 55.409 43.862 104.938 82.646 21.857 17.205 60.134 55.186 103.062 54.955 42.717.231 80.509-37.199 103.053-54.947 49.528-38.783 82.032-64.401 104.947-82.653V400H48z"></path>
</svg>
</fa-icon>
</div>
</mat-list-item>
</app-email-info>
Error:
An unhandled exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll no such element: Unable to locate element: {"method":"xpath","selector":"//p[contains(text(), 'Thank you for registering your email')]"}
The desired element is an Angular element so to Click() on the element you have to induce WebDriverWait for the ElementToBeClickable() and you can use either of the following Locator Strategies:
CssSelector:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("div.mat-list-text>h2"))).Click();
XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[#class='mat-list-text']/h2"))).Click();
Have you tried to click on the item?
driver.FindElement(By.ClassName("ahem-email-list-item")).Click();
Here is a button on the page:
<button data-purpose="add-section-btn" type="button"
class="ellipsis btn btn-default btn-block">
<span class="a3 udi udi-plus-square"></span>
<!-- react-text: 255 --> <!-- /react-text -->
<!-- react-text: 256 -->Add Section<!-- /react-text -->
</button>
When I try to find it using the following code:
var btns = _driver.FindElements(By.TagName("button"));
var sectionTitle = btns.Where(x => x.GetAttribute("data-purpose") == "add-section-btn");
It returns null.
If I try the following XPath:
var btn = _driver.FindElement(By.XPath("//button[data-purpose=\"add-section-btn\"]"));
then I get an exception.
How to find such a button?
Try with,
var btn = _driver.FindElement(By.XPath("//button[#data-purpose='add-section-btn']"));
As per the HTML you have shared to find the element with text as Add Section as the element is a React Element you have to induce WebDriverwait for the element to be visible and you can use either of the following Locator Strategies:
CssSelector:
var btn = new WebDriverWait(_driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.CssSelector("button.ellipsis.btn.btn-default.btn-block[data-purpose='add-section-btn']")));
XPath:
var btn = new WebDriverWait(_driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//button[#class='ellipsis btn btn-default btn-block' and #data-purpose='add-section-btn'][normalize-space()='Add Section']")));
I am trying to automate an environment selection screen where there are multiple selectable buttons individually hidden by a span, these display as tiles.
I have managed to navigate to a given tile and pull up the button but I am unable to click it.
Here is the code I have
public static void NavigateToEnvironment(IWebDriver driver, string environment)
{
IWait<IWebDriver> wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5.00));
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath($"//span[text()='{environment}']")));
var tile = driver.FindElement(By.XPath($"//span[text()='{environment}']"));
Actions action = new Actions(driver);
action.MoveToElement(tile).Perform();
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath($"//*[#span=(text()='{environment}')][#btn=(starts-with(text(), 'Start'))]")));
driver.FindElement(By.XPath($"//*[starts-with(text(), 'Start')]")).Click();
}
The first part successfully moves to the correct tile and opens the span so on screen the button is there.
The wait.until condition is fine too so Selenium can see the element so its the final click command I have an issue with.
It seems only to look for the button hidden within tile one but I am trying tile three. All the buttons have the same HTML tags.
In the current code state I get element not visible.
I have tried to use the xpath as in the wait condition but that returns that the parameters are not elements so again fails.
I am kind of at a loss. Any ideas?
UPDATE:
Some HTML of one of the buttons. This basically repeats with a different application name
<li class="trans tile">
<div class="tileWrap noselect" aria-haspopup="true">
<div class="divNavIcon">
<span class="spnNavIcon primarycolorfont enable" data-bind="css: Code"></span>
</div>
<div class="tilePopup primarycolor">
<span data-bind="text: ApplicationNameAlias ? ApplicationNameAlias : ApplicationName">Enable QA</span>
<span data-bind="text: Description" class="tileSubText">Enable CI Environment</span>
<div class="tilePopupToggle">
<button type="button" data-bind="click: $parent.startApp, css: { disabled: IsRevoked }" class="btn">Start <i class="fa fa-fw fa-desktop"></i></button>
<button type="button" style="display:none;" data-bind="click: $parent.startAppNew, css: { disabled: IsRevoked }" class="btn">Start New <i class="fa fa-fw fa-external-link"></i></button>
<button type="button" style="display:none;" data-bind="attr: { "data-target": "#appPreview_" + ApplicationID }" class="btn" data-toggle="modal" data-target="#appPreview_3043">Preview <i class="fa fa-fw fa-play"></i></button>
</div>
</div>
</div>
Screenshot to help understanding - Each tile acts in the same way with a hidden start button. My code works fine for this first tile but if I want the second or third tiles it cannot find the start button
As per the HTML you have shared to click on the button with text as Start you can use the following code block :
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[#class='tilePopup primarycolor']//div[#class='tilePopupToggle']/button[#class='btn' and normalize-space()='Start']/i[#class='fa fa-fw fa-desktop']"))).Click();
Update
Can you try removing the <button> tag as :
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[#class='tilePopup primarycolor']//div[#class='tilePopupToggle']//i[#class='fa fa-fw fa-desktop']"))).Click();
Note : As per aurelia/binding/issues/163 disable.bind disables button but inner content is still clickable and we are targeting i[#class='fa fa-fw fa-desktop']
I have managed a pretty elegant work around to this issue. The buttons are contained in li items so i'm just finding the relevant one of those.
public void NavigateToEnvironment(IWebDriver driver, string environment)
{
var tile = driver.FindElement(By.XPath($"//span[text()='{environment}']"),5);
Actions action = new Actions(driver);
action.MoveToElement(tile).Perform();
var tile2 = driver
.FindElement(By.XPath("//*[#id='content']/div/div/div/div/ul"))
.FindElements(By.TagName("li"))
.Where(x => !string.IsNullOrWhiteSpace(x.Text))
.ToList();
var singleTile = tile2.Single(x => x.Text.Contains(environment));
driver.FindElement(By.XPath($"//*[#id='content']/div/div/div/div/ul/li[{tile2.IndexOf(singleTile) + 1}]/div[1]/div[2]/div/button[1]")).Click();
}
Actions action = new Actions(driver);
IWebElement we = driver.FindElement(By.XPath(".//*[#class='ms-crm-CommandBar-Button ms-crm-Menu-Label']"));
action.MoveToElement(driver.FindElement(By.XPath(".//*[#class='ms-crm-CommandBar-Button ms-crm-Menu-Label-Hovered']"))).Click().Build().Perform();
expect element as followings:
< span tabindex = "-1" class="ms-crm-CommandBar-Button ms-crm-Menu-Label" style="max-width: 200px;">
<a tabindex = "0" class="ms-crm-Menu-Label" onclick="return false">
<img tabindex = "-1" class="ms-crm-ImageStrip-New_16 ms-crm-commandbar-image16by16" style="vertical-align: top;" src="/_imgs/imagestrips/transparent_spacer.gif">
<span tabindex = "-1" class="ms-crm-CommandBar-Menu" [enter image description here][1]style="max-width: 150px;" command="lead|NoRelationship|HomePageGrid|Mscrm.NewRecordFromGrid">
New
</span>
<div class="ms-crm-div-NotVisible">
Create a new Lead record.
</div>
</a>
</span>
Note that this class "ms-crm-CommandBar-Button ms-crm-Menu-Label" turns to be "ms-crm-CommandBar-Button ms-crm-Menu-Label-Hovered" when mouseover.
Many thanks.
When you search for the hovered element it doesn't exist.
So you need to hover element first, then you'll be able to find and click hovered element.
action.MoveToElement(driver.FindElement(By.XPath("//*[#class='ms-crm-CommandBar-Button ms-crm-Menu-Label']"))).Build().Perform().MoveToElement(driver.FindElement(By.XPath("//*[#class='ms-crm-CommandBar-Button ms-crm-Menu-Label-Hovered']"))).Click().Build().Perform();
In hover case first you need to moveTo That element after that you can click on it.
Actions actions = new Actions(driver);
action.moveToElement(mainMenu).moveToElement(driver.findElement(By.xpath("ur element"))).click().build().perform();
Button's Html
<button id="exist_user" onclick="ExtUpdtPnl();" type="button" style="background-color: rgb(187, 187, 187);">
<i class="fa fa-gear fa-fw"></i>
Update Existing User
</button>
I have used multiple ways as below
driver.FindElement(By.Id("exist_user")).Click();
driver.FindElement(By.XPath("/html/body/form/div[4]/div/div/div[2]/button[1]"));
Please help..
Try driver.FindElement(By.Id("exist_user")).InvokeMember("click"); instead.
Try with JavascriptExecutor:
WebElement element = driver.findElement(By.xpath(".//button[#id='exist_user']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Or
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.getElementById('exist_user').click();");