Element deletion causes wrong click in Selenium - c#

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?

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);
`

Related

Selenium-C#: How to click on a button when it is clickable and not having the disabled attribute in Blazor Web Application

I have a 'save' button which is only clickable when 4 text fields before are filled with text.
In the inspect there are 2 buttons. The Cancel (Abbrechen) button is always clickable, but the save (Speichern) button has an "disabled" parameter in HTML which makes the button not clickable. As soon as the button is clickable, the "Disabled" Parameter disappears.
<div class="mud-dialog-actions">
<!--!-->
<!--!-->
<button type="button" class="mud-button-root mud-button mud-button-text mud-button-text-default mud-button-text-size-medium mud-ripple button" _bl_65608b69-0cf8-4c0e-a2ad-634125333afc=""><span class="mud-button-label">Abbrechen</span></button>
<!--!-->
<!--!-->
<button type="submit" class="mud-button-root mud-button mud-button-text mud-button-text-success mud-button-text-size-medium mud-ripple button button--blue" _bl_8df46032-6965-4980-8c8c-6d3881a66eea="" disabled="">
<span class="mud-button-label">Speichern</span>
</button>
</div>
I tried to use this selenium method:
var element = _webDriverWait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(buttonSelector)));
element.Click();
But this method doesnt help me as it jumps to element.Click() even when button is not clickable and then it throws an exception.
Is there any other way, to check if a button is clickable?
Update
I also tried:
bool elementEnabled = element.Enabled
It returns always true even when the button is not clickable
Invoke click on the element with text Speichern when the disabled attribute is no more present inducing WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategies:
CssSelector:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("div.mud-dialog-actions button[type='submit']:not(disabled)"))).Click();
XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[#class='mud-dialog-actions']//button[#type='submit' and not(#disabled)]"))).Click();

How to click on the element as per the given HTML through Selenium and C#

I want to send instagram shipment with mobile imitation. but I need to click on a button for the new shipment. I tried everything about button click but I did not manage it.
public static void HoverMouseOverClicks(IWebElement element)
{
Actions action = new Actions(insta);
action.MoveToElement(element);
IJavaScriptExecutor executor = (IJavaScriptExecutor)insta;
executor.ExecuteScript("arguments[0].scrollIntoView(true);arguments[0].click();", element);
}
code click-through code you've seen above
HoverMouseOverClicks(insta.FindElement(By.XPath("//*[#id='react-root']/section/nav[2]/div/div/div[2]/div/div/div[3]/span")));
I try this and I still can not get the result.
insta.FindElement(By.XPath("//*[#id='react-root']/section/nav[2]/div/div/div[2]/div/div/div[3]/span")).Click();
This code sometimes clicks and sometimes does not click.
The XPath path never changes.
Relevant HTML:
<div class="q02Nz _0TPg" role="menuitem"><span class="glyphsSpriteNew_post__outline__24__grey_9 Szr5J">Yeni Gönderi</span> <span class="glyphsSpriteNew_post__outline__24__grey_9 Szr5J">Yeni Gönderi</span></div>
what should I do?
As per the HTML you have shared to click on the element with text as Yeni Gönderi you need to induce WebDriverWait for the element to be clickable and you can use the following solution:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[#role='menuitem']//span[contains(#class, 'glyphsSpriteNew_post__') and contains(.,'Yeni Gönderi')]"))).Click();

click function doesnt work with C# and DOM

So basically I want to press a button in a website and it doesn't work. The HTML line looks like this:
<div id="btn-login">
<button type="submit">
<span>SIGN IN THROUGH STEAM</span>
</button>
</div>
And, the code line looks like this:
webBrowser1.Document.GetElementById("btn-login").InvokeMember("click");
When I press the button nothing happens.
The website i'm using is http://csgowitch.com
It looks like the click is currently on the div around the button, not on the button itself. Since the button is the first child of the div, try this:
webBrowser1.Document.GetElementById("btn-login").FirstChild.InvokeMember("click");

trying to click on an webElement which is only visable after clicking on another web elemnt in selenium

i have a page with number of clickable elements which is only visible when clicking on another elements (menus which open and enable another clickable elements)
i want to write a generic code which will enable me to handle a page which contain a lot of drop downs scenario like this.
the HTML look something like this (multiple a few more time)
<div class="mini-menu" g-click="headerClicked(data.leftLink)>
<li ng-repeat="item in items" ng-click="menuItemClick(item)">
</li>
</div>
thanks
If you're using FindBys that aren't cached you should be able to do something like this since the IWebElement isn't found until you use it:
public SomePageObject ClickMenuItem(IWebElement menu, IWebElement item)
{
//click the menu
//wait for item to be visible
//click the item
}
If you're not using FindBys, something like this since you'll have to find the element after it becomes visible:
public SomePageObject ClickMenuItem(IWebElement menu, By item)
{
//click the menu
//Wait for Element to be visible with the item By
//click the item
}
There are a lot of answer out here on how to do the pseudo code part so I'll let you give that a shot. Let me know if you have trouble.

Click a Dropdown Menu Button using Watin

I am testing a website using Watin in the page there is dropdown menu button like if I find that button and click it clicks the entire button and selects the first Option in the menu but I want to click the V of the button to show the dropdown menu(div) and select the option from it the code I had tried to click the button
ff.Button(Find.By("aria-describedby", "x-auto-456")).FireEvent("onmouseover");
ff.Button(Find.By("aria-describedby", "x-auto-456")).FireEvent(" Onmousedown");
ff.Button(Find.By("aria-describedby", "x-auto-456")).Click();
the Button HTML
<button class="x-btn-text" style="position: relative; width: 16px;" type="button" tabindex="0" aria-describedby="x-auto-456">
Edit:
I had tried to find the co-ordinates of the Image in the button and then click it with the below code but its slightly not working kindly any one point me how to click the button and show the div Instead of selecting a option automatically,
NameValueCollection eventProps = new NameValueCollection();
eventProps.Add("clientY", "240");
eventProps.Add("clientX", "240");
ff.Button(Find.By("aria-describedby", "x-auto-456")).FireEvent("onClick",eventProps);
You are not finding the button correctly:
Button button = ff.Button(Find.ByClass("x-auto-457"));
You need to find the button by its class name. Be aware that will find the first <button> or <input type="button|submit|reset"> element with that class name. If you still aren't getting the correct button, check to see how many buttons on the page have x-auto-457 for a class attribute value.

Categories

Resources