I try to listen constantly if a specifc event is triggered, but from what i understand this is not possible using a single or multiple lines of code when using selenium in C#. Instead I have to always use several if statments in between the other selenium operations.
If you know a way to listen if an element is present or not with only a single line or maybe 5, please answer.
For example if I have a code like this:
drivers.Navigate().GoToUrl("someurl");
System.Threading.Thread.Sleep(100);
drivers.Navigate().Refresh();
System.Threading.Thread.Sleep(100);
drivers.Navigate().GoToUrl("someotherurl");
System.Threading.Thread.Sleep(100);
How would I keep listening if each site contains a specifc element without using if statements or specific EvenListenerDriver which i would need to put in between the lines? Bec
CodePudding user response:
You can use EventFiringWebDriver
class from OpenQA.Selenium.Support
Available events:
public event EventHandler<WebDriverNavigationEventArgs> Navigating;
public event EventHandler<WebDriverNavigationEventArgs> Navigated;
public event EventHandler<WebDriverNavigationEventArgs> NavigatingBack;
public event EventHandler<WebDriverNavigationEventArgs> NavigatedBack;
public event EventHandler<WebDriverNavigationEventArgs> NavigatingForward;
public event EventHandler<WebDriverNavigationEventArgs> NavigatedForward;
public event EventHandler<WebElementEventArgs> ElementClicking;
public event EventHandler<WebElementEventArgs> ElementClicked;
public event EventHandler<WebElementValueEventArgs> ElementValueChanging;
public event EventHandler<WebElementValueEventArgs> ElementValueChanged;
public event EventHandler<FindElementEventArgs> FindingElement;
public event EventHandler<FindElementEventArgs> FindElementCompleted;
public event EventHandler<WebDriverScriptEventArgs> ScriptExecuting;
public event EventHandler<WebDriverScriptEventArgs> ScriptExecuted;
public event EventHandler<WebDriverExceptionEventArgs> ExceptionThrown;
And this is an example of how to use it:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.Events;
const string Url = "https://stackoverflow.com/questions/75101509/how-to-use-event-listener-inside-selenium-method-and-call-method-inside-c-sharp#75101509";
using var driver = new ChromeDriver();
driver.Navigate().GoToUrl(Url);
var eventDriver = new EventFiringWebDriver(driver);
eventDriver.ElementValueChanged = OnElementValueChanged;
eventDriver.FindElement(By.XPath("//input[@name= 'q']")).SendKeys("selenium");
// Arbitrary delay in order to demonstrate this example.
// In a real app you should use the `WebDriverWait` class.
await Task.Delay(2000);
eventDriver.FindElement(By.XPath("//input[@name= 'q']")).Clear();
eventDriver.FindElement(By.XPath("//input[@name= 'q']")).SendKeys("playwright");
void OnElementValueChanged(object? sender, WebElementValueEventArgs e)
{
Console.WriteLine(e.Value);
}
// Just to keep active our sample app. In real app
// you will probably use something else
Console.ReadLine();