Selenium unhide elements C# - c#

I have Menu Group:
<div class="menuGroup">
Some of this div contain class which hide or show menu contents
class="toggleMenuChildren">
When you click on it, it change on
class="toggleMenuChildren opened">
So I want show all content from menu (click on all classes toggleMenuChildren) to show it.
I try this
IWebElement zi = driver.FindElement(By.ClassName("toggleMenuChildren"));
zi.Click();
But this opened (unhide) just first element, and if you call it again then hide content. How I can show all content (click on all elements) ?

You can use xpath - //div[contains(#class,'toggleMenuChildren') and not(contains(#class,'opened'))]
(sorry it is in java)
List<WebElement> allElements = driver.findElements(By.xpath("//div[contains(#class,'toggleMenuChildren') and not(contains(#class,'opened'))]"));
for(WebElement ele: allElements){
ele.click;
}

You can try to use hover to show all the information under toggleMenuChildren class
Actions actions = new Actions(driver);
IWebElement menuHoverLink =
driver.FindElement(By.XPath("//div[#class='toggleMenuChildren']"));
actions.MoveToElement(menuHoverLink);
actions.Build().Perform();
//That is the elements under the toggleMenuChildren class that you can use
driver.FindElement(By.PartialLinkText("...")).Click();
after perform() line all the elements will be unhided .

Related

How can i select the option of ul list item with selenium C#

How can i select item of ul list with selenium with C# in web page . Currently i have web page i can access the other control by id of that but this select control are not able to work with me what i have tried
what i have tried is
IWebElement elements = driver.FindElement(By.ClassName("ant-select-dropdown-menu"));
SelectElement ses = new SelectElement(elements);
ses.SelectByText("Web Project");
The Error Message is
no such element: Unable to locate element: {"method":"css selector","selector":".ant-select-dropdown-menu"}
This drop down is not built up using Select and option tag (HTML), so we can not use Select class from selenium, we will have to click on this drop down first and then store all options in a list and then iterate through the list and using if clause (if desired text is found) click on it.
Click on the drop down like this :
IWebElement elements = driver.FindElement(By.CssSelector("ul[class*='ant-select-dropdown-menu']"));
elements.Click();
and then use FindElements
IList<IWebElement> selectElements = driver.FindElements(By.CssSelector("ul[class*='ant-select-dropdown-menu'] li"));
For(WebElement ele : selectElements){
if(ele.GetText() == "Desktop project")
ele.Click();
}
Thank You I Solve my problem with this
IWebElement element = driver.FindElement(By.Id("selectid"));
element.Click();
Thread.Sleep(1000);
var slctOption =
driver.FindElement(By.XPath("//li[contains(text(),'Desktop
Project')]"));
slctOption.Click();

How can i use click event in Selenium and C#

How can I use click event in Selenium and C#
For example:
Go to Google Homepage.
Search the result Test
Click on the Search button, page change.
How can I get the new page?
Code Screenshot link: https://www.dropbox.com/s/23ih019wuczp3uv/Screenshot%202018-05-07%2011.08.38.png?dl=0
public void opengoogle()
{
ChromeOptions option = new ChromeOptions();
option.AddArgument("--headless");
ChromeDriver wd = new ChromeDriver(option);
try
{
wd.Navigate().GoToUrl("https://www.google.co.in/");
Thread.Sleep(2000);
wd.FindElement(By.CssSelector("#lst-ib")).Click();
Thread.Sleep(2000);
wd.FindElement(By.CssSelector("#lst-ib")).Click();
wd.FindElement(By.CssSelector("#lst-ib")).Clear();
wd.FindElement(By.CssSelector("#lst-ib")).SendKeys("Test");
}
finally { }
If you observe the HTML the desired element identified through By.CssSelector("#lst-ib"), it is within a <form> tag. So once you have sent the search text within the search field you can invoke Submit() method as follows :
wd.FindElement(By.CssSelector("#lst-ib")).Click();
wd.FindElement(By.CssSelector("#lst-ib")).Clear();
wd.FindElement(By.CssSelector("#lst-ib")).SendKeys("Test");
wd.FindElement(By.CssSelector("#lst-ib")).Submit();

Spinner clear value exception in Selenium c# Webdriver

I want to test this popup window:
but for spinner I get exception. I tried these codes but non of them worked.
Actions componentProportion = new Actions(driver);
componentProportion.SendKesy(Kesy.Control+"a");
componentProportion.SendKeys(editNumber,"2356").Build().Perform();
When I run this code nothing happens and it also causes problem for new text box(Display Title) code and it doesn't work properly.
//Edit display title
IWebElement editDt = driver.FindElement(By.XPath("//*[#id='cmp_display_title']"));
editDt.Click();
Thread.Sleep(500);
editDt.SendKeys(Keys.Control + "a");
editDt.SendKeys(Keys.Delete);
Thread.Sleep(500);
editDt.SendKeys("hello");
And if I want to use this code:
IWebElement editNumber = driver.FindElement(By.CssSelector("something"));
editNumber.Click();
Thread.Sleep(1000);
editNumber.SendKeys(Keys.Control+"a");
I get this exception:
ElementVisibleException : element not visible
Is there anyway to solve this problem? Thank you for your help.
So, since your elements are in frame, in order to be able to work with them you need to switch to that frame first (usually by name)
//Switch to required frame
driver.SwitchTo().Frame("ContentContainer");
// find your elements and do the actions
// This is how you switch to the default page
driver.SwitchTo().DefaultContent();
As I noticed, you want to replace the display title input text with some new text
In order to clear the text, you dont need to simuale ctrl+a.
Simply, use this code to set new value to your text element :
IWebElement editDt = driver.FindElementById("cmp_display_title");
editDt.SendKeys(""); // clears the text
editDt.SendKeys("your new text");

Programmatically click button in C#

I'm trying to programmatically click a series of buttons on an HTML web page which looks as follows:
<div class="srp-actions blue-button"><a class="primary-action-button label" href="/people/invite?from=profile&key=243930744&firstName=Will&lastName=Yang&authToken=p8Oz&authType=OUT_OF_NETWORK&connectionParam=member_desktop_search_people-vertical-module&csrfToken=ajax%3A7824954558998584370&trk=vsrp_people_res_pri_act&trkInfo=VSRPsearchId%3A12487701484818103943%2CVSRPtargetId%3A243930744%2CVSRPcmpt%3Aprimary" data-li-result-interaction="instant-connect" data-li-success-text="Invite Sent" data-li-connect-href="/people/contacts-search-invite-submit?memIds=243930744&authTokens=p8Oz&authTypes=OUT_OF_NETWORK&from=voltron&firstName=Will&lastName=Yang&isAjax=true&connectionParam=member_desktop_search_people-vertical-module&csrfToken=ajax%3A7824954558998584370&trk=vsrp_people_res_invite_act&trkInfo=VSRPsearchId%3A12487701484818103943%2CVSRPtargetId%3A243930744%2CVSRPcmpt%3Aprimary">Connect</a><div class="secondary-actions-trigger"><button role="button" class="trigger"><span>Secondary Actions</span></button><ul class="menu"><li>Send InMail</li><li>Share</li></ul></div></div>
Here's the current code to find the button element and perform the action:
HtmlElementCollection elements = webBrowser1.Document.GetElementsByTagName("a");
// First find and click "Connect" buttons
foreach (HtmlElement item in elements)
{
if (item.OuterHtml.Contains("action-button label") &&
!item.OuterHtml.Contains("Message") &&
item.OuterHtml.Contains("OUT_OF_NETWORK"))
{
item.SetAttribute("href", item.GetAttribute("data-li-connect-href"));
item.InvokeMember("Click");
}
}
The code properly find the anchor element, but the InvokeMember method doesn't seem to yield any result, any idea what is wrong?
The tag you are using does not specify exactly which specific tag you need:
x = webBrowser1.Document.GetElementsByTagName("a")
Instead try :
x= webBrowser1.Document.GetElementsById("anchor_id");
x.InvokeMember("click");
Or try using following method to verify whether its the intended tag you're using via attribute.
if (element.GetAttribute(attribute).Equals(attName))

selenium unable to find element button

i am trying to get this button with selenium in c#
<a id="1|0AqnCSdkjQ0|none" href="" target="_self" rel="nofollow" class="download_link 1">Download</a>
i tried with id and class but it didn't work.
Here is the web page:
http://www.mp3juices.cc/ - > on the next page
Your code is failing with that "Compound class" error because you are, basically, asking for two class names.
The button has the class download_link.
If you do something like driver.findElements(By.className("download_link")) you'll get a List of all the buttons, and get whichever you wanted.
(The above snippet is Java, so you may have to adapt it to C#)
you can use this as a solution
driver.findElement(By.xpath("//a[#class='download_link 1'] and contains(text(),'Download')"));
Did you try
driver.FindElement(By.CssSelector("a.download_link.2")
When you tried to use "download_link 2" you were requesting two class names. You can specify two class names (or more) in a CSS Selector and put a period between them. The CSS selector above is read as find an A tag with class download_link and class 2.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
try
{
IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
{
try
{
return d.FindElement(By.ClassName("dl_link 1"));
}
catch
{
return null;
}
});
}
catch(Exception e)
{
}
ReadOnlyCollection<IWebElement> lists = driver.FindElements(By.ClassName("download_link"));
lists[0].Click();
*The code is not optimized, but it works great. (First part i am using to wait for the button to be loaded).

Categories

Resources