0

When selenium tries to click a button, web site deleting a div above, and selenium clicks the element under the element that I wanted to click. I try to click with XPath, CSS selector, and the class name and none of them work. Also, I tried action chains to click but not worked.

And before clicking, I can confirm the selected element is the right element.

var element=driver.FindElements(By.ClassName("warning"))[0];
Debug.WriteLine(element.Text);

This writes the right element to the output.

-Here is the code By.ClassName:

driver.FindElements(By.ClassName("warning"))[0].Click();

The element under the right element does not have a "warning" class but it's still clicking on that element.

The page is basically like this:

<div id="maindiv">
<div class="the div that will delete"></div>
<button class="warning"></button>
<button class="second"></button>
<button class="third"></button>
<button class="fourth"></button>
</div>

here is how its looks

Basically, I'm trying to click the "warning" button but the "third" button receiving the click. What should I do?

1 Answer 1

0

I solved the problem. First I deleted the div before clicking, then I clicked the button. Here is the code:

var element = driver.FindElement(By.XPath("//[@id=\"app\"]/div[7]/main/div/div/div[2]/div/div/div[1]/div[1]"));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].parentNode.removeChild(arguments[0]);",element );

var button=driver.FindElement(By.XPath("//*[@id=\"app\"]/div[7]/main/div/div/div[2]/div/div/div[1]/span[1]/span/button"));
((IJavaScriptExecutor) driver).ExecuteScript("arguments[0].click();",button);

     `
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.