I want to select My jobs tag and then on that but there's no Id or Name for given tag just a href but when i am trying to find my cssSelector or by cssClass then it's shows erorr.So any one who can help me
enter image description here
For clicking on My Jobs , you can use this code:
IWebElement element = driver.FindElement(By.LinkText("My Jobs"));
element.Click();
For clicking on New Jobs , you can use this code:
IWebElement element = driver.FindElement(By.LinkText("New Jobs"));
element.Click();
For clicking on My Units , you can use this code:
IWebElement element = driver.FindElement(By.LinkText("My Units"));
element.Click();
For clicking on Timesheets , you can use this code:
IWebElement element = driver.FindElement(By.LinkText("Timesheets"));
element.Click();
Hope this will work for you!
Please let me know if you have any concerns related to this.
Related
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();
I'm using Selenium.WebDriver for C# to ask a question on Quora just typing my question in notepad.
Everything worked fine since I had to post it.
To post it I need to click on a link inside a span like this:
<span id="__w2_wEA6apRq1_submit_question">
<a class="submit_button modal_action" href="#" id="__w2_wEA6apRq1_submit">Add Question</a>
</span>
In order to click it I've tried this method, that I've used for all my previous button clicks:
Selecting the element and clicking it:
var element = driver.FindElement(By.CssSelector(".submit_button.modal_action"));
element.Click();
Doing so I can get the element, but unfortunately it throws "ElementNotVisibleException". Debugging my application I could see that Displayed property was set to False, while it wasn't, because in my ChromeDriver I could clearly see the button.
To avoid clicking the element, I tried IJavaScriptExecutor and Driver.ExecuteJavaScript(); to click the link through a script:
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("arguments[0].click()", element);
Same logic has been used for Driver.ExecuteJavaScript(); but I get the same result, but when I write the same script into DevTools's Console tab it works perfectly.
How can I solve this issue?
You might have a case where the button become displayed(visible) after your check is executed, so you might try following delay in order to ensure that the button is displayed at the time of check:
public static void WaitForElementToBecomeVisibleWithinTimeout(IWebDriver driver,
IWebElement element, int timeout)
{
new WebDriverWait(driver,
TimeSpan.FromSeconds(timeout)).Until(ElementIsVisible(element));
}
private static Func<IWebDriver, bool> ElementIsVisible(IWebElement element)
{
return driver =>
{
try
{
return element.Displayed;
}
catch (Exception)
{
// If element is null, stale or if it cannot be located
return false;
}
};
}
If the button is not visible in the viewport(i.e. need scrolling to become visible) then you may scroll it with
public static void ScrollElementToBecomeVisible(IWebDriver driver, IWebElement element)
{
IJavaScriptExecutor jsExec = (IJavaScriptExecutor)driver;
jsExec.ExecuteScript("arguments[0].scrollIntoView(true);", element);
}
As per the HTML you have shared to click on the element with text as Add Question as the element is within a Modal Dialog you need to induce WebDriverWait for the desired ElementToBeClickable and you can use either of the following Locator Strategies as solutions:
LinkText:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.LinkText("Add Question"))).Click();
CssSelector:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("span[id$='_submit_question']>a.submit_button.modal_action"))).Click();
XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//span[contains(#id,'_submit_question')]/a[#class='submit_button modal_action' and contains(.,'Add Question')]"))).Click();
Hy,
on a website there are some button:
Previously I copied the old code to the question, this is the actual code:
IWebElement btn_tag_1 = driver.FindElement(By.XPath("//*[#data-testid ='tag_eject']"));
IWebElement btn_tag_2 = driver.FindElement(By.XPath("//*[#data-testid ='tag_close']"));
IWebElement btn_tag_3 = driver.FindElement(By.XPath("//*[#data-testid ='tag_open']"));
IWebElement btn_tag_4 = driver.FindElement(By.XPath("//*[#data-testid ='tag_selected']"));
IWebElement btn_tag_5 = driver.FindElement(By.XPath("//*[#data-testid ='tag_disabled']"));
IWebElement btn_tag_6 = driver.FindElement(By.XPath("//*[#data-testid ='tag_free']"));
In a normal case when I would like to click a selected element for example "btn_tag_1" the code is:
btn_tag_1.Click();
But now I wanted to click one of them selected randomly and I'm totally stucked at this point.
Can You help me in this case?
Thank You!
If you are just looking for some help on how to randomly click one of the buttons then I would do it this way:
using System;
using OpenQA.Selenium;
By[] buttonsBy = {
By.XPath("//*[#data-testid='tag_1']"),
By.XPath("//*[#data-testid='tag_2']"),
By.XPath("//*[#data-testid='tag_3']"),
By.XPath("//*[#data-testid='tag_4']"),
By.XPath("//*[#data-testid='tag_5']"),
By.XPath("//*[#data-testid='tag_6']")
};
int index = new Random().Next(buttonsBy.Length - 1);
IWebElement button = driver.FindElement(buttonsBy[index]);
button.Click();
Create an array with your By selectors, choose a random number within the length of that array, find the element using that random number as the index and then click.
Added after comment with code:
All you need to do is change the string inside each locator to use the code in your comment.
By[] buttonsBy = {
By.XPath("//*[#data-testid='tag_eject']"),
By.XPath("//*[#data-testid='tag_close']"),
By.XPath("//*[#data-testid='tag_open']"),
By.XPath("//*[#data-testid='tag_selected']"),
By.XPath("//*[#data-testid='tag_disabled']"),
By.XPath("//*[#data-testid='tag_free']")
};
I'm using Selenium.WebDriver for C# to ask a question on Quora just typing my question in notepad.
Everything worked fine since I had to post it.
To post it I need to click on a link inside a span like this:
<span id="__w2_wEA6apRq1_submit_question">
<a class="submit_button modal_action" href="#" id="__w2_wEA6apRq1_submit">Add Question</a>
</span>
In order to click it I've tried this method, that I've used for all my previous button clicks:
Selecting the element and clicking it:
var element = driver.FindElement(By.CssSelector(".submit_button.modal_action"));
element.Click();
Doing so I can get the element, but unfortunately it throws "ElementNotVisibleException". Debugging my application I could see that Displayed property was set to False, while it wasn't, because in my ChromeDriver I could clearly see the button.
To avoid clicking the element, I tried IJavaScriptExecutor and Driver.ExecuteJavaScript(); to click the link through a script:
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("arguments[0].click()", element);
Same logic has been used for Driver.ExecuteJavaScript(); but I get the same result, but when I write the same script into DevTools's Console tab it works perfectly.
How can I solve this issue?
You might have a case where the button become displayed(visible) after your check is executed, so you might try following delay in order to ensure that the button is displayed at the time of check:
public static void WaitForElementToBecomeVisibleWithinTimeout(IWebDriver driver,
IWebElement element, int timeout)
{
new WebDriverWait(driver,
TimeSpan.FromSeconds(timeout)).Until(ElementIsVisible(element));
}
private static Func<IWebDriver, bool> ElementIsVisible(IWebElement element)
{
return driver =>
{
try
{
return element.Displayed;
}
catch (Exception)
{
// If element is null, stale or if it cannot be located
return false;
}
};
}
If the button is not visible in the viewport(i.e. need scrolling to become visible) then you may scroll it with
public static void ScrollElementToBecomeVisible(IWebDriver driver, IWebElement element)
{
IJavaScriptExecutor jsExec = (IJavaScriptExecutor)driver;
jsExec.ExecuteScript("arguments[0].scrollIntoView(true);", element);
}
As per the HTML you have shared to click on the element with text as Add Question as the element is within a Modal Dialog you need to induce WebDriverWait for the desired ElementToBeClickable and you can use either of the following Locator Strategies as solutions:
LinkText:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.LinkText("Add Question"))).Click();
CssSelector:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("span[id$='_submit_question']>a.submit_button.modal_action"))).Click();
XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//span[contains(#id,'_submit_question')]/a[#class='submit_button modal_action' and contains(.,'Add Question')]"))).Click();
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 .