1

This is my code in C# I press the delete button and open a pop up where I select the amount of deletions that I want to delete and press again on delete it will delete.

PropertiesCollections.driver.FindElement(By.LinkText("Delete")).Click();
new SelectElement(PropertiesCollections.driver.FindElement(By.XPath("(//select[@id='remove_shares'])[2]"))).SelectByText("1");
PropertiesCollections.driver.FindElement(By.XPath("(//button[@type='button'])[2]")).Click();

What I'm trying to do is record a loop every time I press delete and open a pop-up window, find the highest value inside the dropdown and select it and delete.

But just every time the values inside the dropdown are changed:

PropertiesCollections.driver.FindElement(By.LinkText("Delete")).Click();
var mySelectElm = PropertiesCollections.driver.FindElement(By.XPath("(//select[@id='remove_shares'])[2]"));
var mySelect = new SelectElement(mySelectElm);
var values = mySelect.SelectedOption;
foreach (var option in values)
{
 Console.WriteLine(option.Text); //Prints "Option", followed by "Not Option"
 PropertiesCollections.driver.FindElement(By.XPath("(//button[@type='button'])[2]")).Click();
 }

That the error I get but I need help with writing the code better:

Error 1

foreach statement cannot operate on variables of type 'OpenQA.Selenium.IWebElement' because 'OpenQA.Selenium.IWebElement' does not contain a public definition for 'GetEnumerator'

C:\Users\Numg\Desktop\selenium\ConsoleApplication5\ConsoleApplication5\Program.cs 54 17 ConsoleApplication5

Bulat
3003 silver badges9 bronze badges
asked Apr 1, 2017 at 7:31

1 Answer 1

3

Replace this:

var values = mySelect.SelectedOption;

With this:

var values = mySelect.Options;

That way your foreach loop will work, because you'll have a list of options to loop through. Your current code only returns a single element, on which foreach does not work.

Niels van Reijmersdal
32.7k4 gold badges59 silver badges125 bronze badges
answered Apr 1, 2017 at 12:09
3
  • when i changed to var values = mySelect.Otions; get error message Error 1 'OpenQA.Selenium.Support.UI.SelectElement' does not contain a definition for 'Otions' and no extension method 'Otions' accepting a first argument of type 'OpenQA.Selenium.Support.UI.SelectElement' could be found (are you missing a using directive or an assembly reference?) C:\Users\Numg\Desktop\selenium\ConsoleApplication5\ConsoleApplication5\Program.cs 53 39 ConsoleApplication5 Commented Apr 1, 2017 at 12:22
  • Really you didnt notice the spelling error? and the missing p... lol Commented Apr 1, 2017 at 12:24
  • sorry my bud,but now i get error message Test Name: ADDITEMTOCART Test Outcome: Failed Result Message: System.InvalidOperationException : unknown error: Element <a class="button delete-share" data-id="delete-0">...</a> is not clickable at point (921, 539). Other element would receive the click: <span>...</span> (Session info: chrome=56.0.2924.87) (Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 10.0.14393 x86_64) Result StandardOutput: opened url close the broswer Commented Apr 1, 2017 at 17:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.