Home > database >  How to hover over an item avoiding a click on it using Selenium and C#
How to hover over an item avoiding a click on it using Selenium and C#

Time:11-25

I have a problem, I want to hover over a certain button (to develop) but I don't want to click it, I don't know how to do it. when I click this button, it takes me to another place and I don't want it

 private void button1_Click(object sender, EventArgs e)
        {
            string url = textBox1.Text;
            driver.Navigate().GoToUrl(url); Thread.Sleep(2000);

            driver.FindElement(By.XPath("//button[@id='onetrust-accept-btn-handler']")).Click(); Thread.Sleep(3000);

            driver.FindElement(By.XPath("//a[normalize-space()='Mój OLX']")).Click(); Thread.Sleep(2000); 

            driver.FindElement(By.XPath("//section[@class='login-page has-animation']//input[@id='userEmail']")).SendKeys("[email protected]"); Thread.Sleep(3000);

            driver.FindElement(By.XPath("//input[@id='userPass']")).SendKeys("Anastazja12345");

            driver.FindElement(By.XPath("//section[@class='login-page has-animation']//button[@id='se_userLogin']")).Click(); Thread.Sleep(3000);

            driver.FindElement(By.XPath("//div[@class='css-1povu0j']"));


            //driver.FindElement(By.XPath("//a[normalize-space()='Wyloguj']")).Click();
            //driver.FindElement(By.XPath("")).Click();


        }

CodePudding user response:

You can use the Action after you find the element you want to hover

    var element = driver.FindElement(By.XPath("//div[@class='css-1povu0j']"));
    Actions action  = new Actions(driver);
    action.MoveToElement(element).Perform();

CodePudding user response:

To hover over a certain button but to avoid clicking it you have to induce WebDriverWait for the desired ElementIsVisible() and the use Actions Class methods as follows:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
var element = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//a[normalize-space()='Wyloguj']")));

Actions action  = new Actions(driver);
action.MoveToElement(element).Perform();

References

You can find a couple of relevant detailed discussions in:

  • Related