I am working on selenium web driver using the language "Java" and want to access two elements of same classname. Actually, both the elements are error messages which are coming in small popup having the same class. But the problem is that every time it only picks the first element of the class which is coming. Please suggest which method I should use to get both the elements.
Also, I need to compare both the messages with the string that I have added. Here is the code I have tried:
public class mysignup {
public static WebDriver d;
public static void main(String []args)throws Exception{
d = new FirefoxDriver();
d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
d.findElement(By.name("firstname")).sendKeys("qwertyuiokjhgfdsazxcvbnmkloiuytr");
d.findElement(By.name("firstname")).click();
d.findElement(By.name("lastname")).sendKeys("singh");
d.findElement(By.name("email_id")).sendKeys("abcgmail.com");
d.findElement(By.name("firstname")).click();
d.findElement(By.name("email_id")).click();
String bodyText = d.findElement(By.cssSelector(".popover-content")).getText();
While findElement returns you a single WebElement, findElements will return all elements that match given conditions.
In such a scenario I would suggest using findElements method. It will return you a list of all elements if found or an empty list. So you can try out with:
List<WebElement> lstEle = d.findElements(By.cssSelector(".popover-content"));
List<String> strLst = new ArrayList<String>();// list to contain all texts in each element
lst.forEach(new Consumer<WebElement>() { // foreach element add text to strLst
#Override
public void accept(WebElement t) {
strLst.add(t.getText());
}
});
Related
I have a code which reads multiple string-arrays out of a dynamical dll. I'm creating GameObjects out of each string inside that array and i want to group each array of strings as unique groups.
So far, I've managed to give theese GameObject specific tags to group them, but theese tags were pre-created in the Unity Editor. This works so far, but in case the dynamic class provides more arrays than tags i have created previously, it won't be able to group the additional arrays.
My question: is there any way to create new tags via script while the game is running? With theese i could just simply add them to the new GameObjects.
In case that this shouldn't be possible, does someone have an idea of a different way to sort theese groups?
Thanks in advance!
In principle, the tag system is just a Dictionary of Lists. The problem is that Unity's Dictionary us defined at compile-time. The downside of rolling your own Tag system is you need some way to clear destroyed objects.
public class CustomTag : MonoBehaviour
{
private static Dictionary<string, List<GameObject>> Collection = new Dictionary<string, List<GameObject>>();
public static void Register(string tag, GameObject item) {
if (!Collection.ContainsKey(tag))
Collection.Add(tag, new List<GameObject>());
Collection[tag].Add(item);
}
public static void Deregister(string tag, GameObject item) {
if (!Collection.ContainsKey(tag))
return; // No Such Tag
// In case of multiple entries, remove all occurences of item
// If you're sure you will only have one entry per item, you can just use 'Remove'
Collection[tag].RemoveAll(m => m == item);
}
public static IEnumerable<GameObject> FindObjectsWithTag(string tag) {
if (!Collection.ContainsKey(tag))
return null;
return Collection[tag].AsEnumerable();
}
public string Tag;
private void Start() {
Register(Tag, gameObject);
}
private void OnDestroy() {
Deregister(Tag, gameObject);
}
}
Add this component to each GameObject you create, and set the Tag. It will take care of registering and deregistering. You can access the list of objects per tag using CustomTag.FindObjectsWithTag(). Note that with the current setup, objects won't register with the tag system until they Awake (which IIRC won't be until just before next update)
I need to get the WebElement name(Userdefine name) for reporting Purpose. Performing Click operation on AddMainConcernLink : If The element is Clicked/not .I need to report "AddMainConcernLink" is Clicked/not found
[FindsBy(How = How.CssSelector, Using = "[data-test-id='ECNMainConcernsLink']")]
private IWebElement AddMainConcernLink;
public void Click(IWebElement element)
{
element.Click();
Console.WriteLine("Perfomed click operation on element : " + element);
}
I want to Print Perfomed click operation on Element: AddMainConcernLink.
I simply pass a descriptive parameter along with the web element to my helper routines. For example, in a calculator Android app, I have the following:
public void clickDegreeRadsToggle() {
helper.click(degreeRadsToggle, "Degree/Rads Toggle");
}
And the helper.click method then logs using the passed description.
Currently I am using #FindBy annotation for an element as below
#FindBy(xpath = "//a[#class='fNiv' and contains(text(), 'Home')]")
public static WebElement Tab_Home;
But when I try using Boolean I am getting an error "Change to getSize()" and my Boolean syntax is
//AllGeneralTabs is the class name where I have stored my elements
Boolean home=AllGeneralTabs.Tab_Home.size()>0;
I want something to work in such a way when I define my Element as
public static final By Tab_Home=By.xpath("//a[#class='fNiv' and contains(text(), 'Home')]");
then the Boolean will work perfectly for the below syntax
Boolean home=driver.findElements(AllGeneralTabs.Tab_Home).size()>0;
It returns true/False and works as expected but this is not happening for #FindBy annotation. As I defined all my elements using #FindBy now and I cannot go back and change it to final statement, I don't have time.
Try below solutions.
#FindAll(#FindBy(how = How.XPATH, using = "//a[#class='fNiv' and contains(text(), 'Home')]"))
List<WebElement> allElements;
OR
#FindBys(#FindBy(xpath="//a[#class='fNiv' and contains(text(), 'Home')]")))
List<WebElement> allElements;
Let me know if it works for you.
You are mixing between WebElement size witch return Dimension and List size witch return int. You didn't say what exactly you are trying to do, but it seems you are looking for
#FindBy(xpath = "//a[#class='fNiv' and contains(text(), 'Home')]")
public static List<WebElement> Tab_Home;
Hi I need to access to a Select Field component from the Material UI library. I'm using the traditional way but as expected is throwing an error because this library generates div elements instead of select.
Please any idea of how to select elements with this component?
WebSite Url: http://www.material-ui.com/#/components/select-field
Error: "org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "div".
The code I used is the follow:
public class MaterialUITest {
private WebDriver driver;
By selectFieldLocator = By.xpath("//div[contains(#id,'undefined-undefined-Frequency')]/div[1]/div[2]");
#Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "./src/test/resources/drivers/chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.material-ui.com/#/components/select-field");
}
#After
public void tearDown() throws Exception {
}
#Test
public void test() {
WebElement selectField = driver.findElement(selectFieldLocator);
Select dropdown = new Select(selectField);
dropdown.selectByVisibleText("Weekly");
WebElement option = dropdown.getFirstSelectedOption();
System.out.println(option.getText());
}
}
As I understand problem that you're having is not related to material itself it's more related to custom implementation of select, i.e. this is not select, you should treat this 'select' as regular web element and handle it respectively in other words you need to click on it to expand and then perform another click on required element to select it.
Inspect element and copy full xpath.
Try with Full xpath it will work you need take the xpath of full tab because inner elements are not enabled because of parent elements.
I have tried to use Html Elements framework. Here are one of my blocks:
#Block(#FindBy(id = "test"))
public class FirstBlock extends HtmlElement {
#FindBy(id = "nameS")
private TextInput id;
#FindBy(id = "saveBt")
private Button add;
public void addNewClient(String idText) {
add.click();
id.sendKeys(idText);
}
}
I have initialized page factory like:
PageFactory.initElements(new HtmlElementDecorator(driver), this);
Now I want to wait after add.click(); until next element is present.
As I found out where is possibility to use AjaxElementLocatorFactory
But how can I make this using Html Elements framework?
HtmlElements use AjaxElementLocatorFactory by default, so you don't need any explicit waits in your code. It will try to find your id element until succeed and then executes sendKeys() on it. In case element wait timeout will be reached, it'll throw ElementNotFound exception.