Selenium C How To Click On A Button When It Is Clickable And Not Having The Disabled Attribute In Blazor Web Application

Find the data you need here

We provide programming data of 20 most popular languages, hope to help you!

Previous PostNext Post

<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>
var element = _webDriverWait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(buttonSelector)));
element.Click();
bool elementEnabled = element.Enabled
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("div.mud-dialog-actions button[type='submit']:not(disabled)"))).Click();
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[@class='mud-dialog-actions']//button[@type='submit' and not(@disabled)]"))).Click();

Selenium thinks button is clickable even when disabled

I'm having an issue where Selenium is saying a button is clickable even when it's disabled. I'm using Selenium with a website where you have to select a date first and then time slot from a dropdown list before the "Book" button is clickable and will actually do anything. Before the date and time slot are chosen, the button element is

 <div id="pt1:b2" class="x28o xfn p_AFDisabled p_AFTextOnly" style="width:300px;" _afrgrp="0" role="presentation"><a data-afr-fcs="false" class="xfp" aria-disabled="true" role="button"><span class="xfx">Book</span></a></div>
<div id="pt1:b2" class="x28o xfn p_AFTextOnly" style="width:300px;" _afrgrp="0" role="presentation"><a href="#" onclick="this.focus();return false" data-afr-fcs="true" class="xfp" role="button"><span class="xfx">Book</span></a></div>
    wait = WebDriverWait(driver, 10)
    wait.until(EC.element_to_be_clickable((By.ID, 'pt1:b2')))

How to click a button on webpage using selenium ?

Selenium can automatically click on buttons that appear on a webpage. This article revolves around how to click any button using Selenium in a webpage. In order to do this there are two major steps we have to take : Find the button. Click on the button. We can find the button on the web page by using methods like find_element_by_class_name

# finding the button using ID
button = driver.find_element_by_id(ID)

# clicking on the button
button.click()

How to use the button onclick event in Blazor WebAssembly

Adding a button onclick event. We start with adding a button click event, and for this, we are going to use a textarea to bind a value to a property. Afterwards, we will create a button. We will use button binding to a call method that …

// Note.cs
public class Note
{
	public string Message { get; }

	public DateTimeOffset Created { get; }

	public Note(string message)
	{
		Message = message;
		Created = DateTimeOffset.UtcNow;
	}
}
<!-- NoteViewComponent.razor -->
@if (Note != null)
{
    <li>
        <span>@Note.Message</span>
        <span>Created: @Note.Created.ToUniversalTime().ToString("ddd d MMM yyyy HH:mm:ss")</span>
    </li>
}
@code {
    [Parameter]
    public Note Note { get; set; }
}
<!-- NoteListingComponent.razor -->
@page "/"
<div class="col-6">
	<h2>Enter your note</h2>
	<fieldset>
		<label for="Comment">
			<textarea id="Comment" cols="50" rows="6"></textarea>
		</label>
	</fieldset>
	<button type="submit">Submit</button>
</div>
<div class="col-6">
	<h2>Your saved notes</h2>
	@if (Notes?.Any() ?? false)
	{
		<ul>
			@foreach (var note in Notes)
			{
				<NoteViewComponent Note="@note"></NoteViewComponent>
			}
		</ul>
	}
	else
	{
		<p>You currently do not have any saved notes.</p>
	}
</div>
@code {

	public IList<Note> Notes { get; set; }

	protected override async Task OnInitializedAsync()
	{
		Notes = new List<Note>();

		await base.OnInitializedAsync();
	}
}
<!-- NoteListingComponent.razor -->
@page "/"
...
@code {
	...	
	public string NewComment { get; set; }
	...
}
<!-- NoteListingComponent.razor -->
@page "/"
<div class="col-6">
	<h2>Enter your note</h2>
	<fieldset>
		<label for="Comment">
			<textarea id="Comment" cols="50" rows="6" @bind="NewComment" @bind:event="onchange"></textarea>
		</label>
	</fieldset>
	<button type="submit">Submit</button>
</div>
...
@code {
...
}
<!-- NoteListingComponent.razor -->
@using RoundTheCode.BlazorOnClick.Models
@page "/"
...
@code {

	public IList<Note> Notes { get; set; }

	...
	
	protected void OnSubmitNote(MouseEventArgs mouseEventArgs)
	{
		Notes.Add(new Note(NewComment));
		NewComment = string.Empty;            
	}

	...
}
<!-- NoteListingComponent.razor -->
@page "/"
<div class="col-6">
	...
	<button type="submit" @onclick="@OnSubmitNote">Submit</button>
</div>
...
@code {
...
}
<!-- NoteViewComponent.razor -->
...
@code {
	...
    [Parameter]
    public EventCallback<MouseEventArgs> OnDeleteNote { get; set; }
}
<!-- NoteViewComponent.razor -->
@if (Note != null)
{
    <li>
        <span>@Note.Message</span>
        <span>Created: @Note.Created.ToUniversalTime().ToString("ddd d MMM yyyy HH:mm:ss")</span>
        <button type="submit" @onclick="@OnDeleteNote">Delete</button>
    </li>
}
@code {
...
}
<!-- NoteListingComponent.razor -->
@page "/"
...
@code {

    ...

    protected void OnDeleteNote(MouseEventArgs mouseEventArgs, Note note)
    {
        if (Notes?.Any(n => n == note) ?? false)
        {
            Notes.Remove(Notes.First(n => n == note));
        }
    }
}
<!-- NoteListingComponent.razor -->
...
<div class="col-6">
    <h2>Your saved notes</h2>
    @if (Notes?.Any() ?? false)
    {
        <ul>
            @foreach (var note in Notes)
            {
                <NoteViewComponent Note="@note" OnDeleteNote="@((e) => OnDeleteNote(e, note))"></NoteViewComponent>
            }
        </ul>
    }
    else
    {
        <p>You currently do not have any saved notes.</p>
    }
</div>
@code {
	...
}
<!-- NoteViewComponent.razor -->
@if (Note != null)
{
    <li>
        <span>@Note.Message</span>
        <span>Created: @Note.Created.ToUniversalTime().ToString("ddd d MMM yyyy HH:mm:ss")</span>
        <button type="submit" @onclick="@OnDeleteNote">Delete</button>
    </li>
}
@code {
    public string ClassName { get; set; }
    ...
}
<!-- NoteViewComponent.razor -->
@using RoundTheCode.BlazorOnClick.Models
@if (Note != null)
{
    <li class="@ClassName">
        <span>@Note.Message</span>
        <span>Created: @Note.Created.ToUniversalTime().ToString("ddd d MMM yyyy HH:mm:ss")</span>
        <button type="submit" @onclick="@OnDeleteNote">Delete</button>
    </li>
}
@code {
...
}
/* NoteViewComponent.razor.css */
.highlight {
    background-color: #ccc;
}
<!-- NoteViewComponent.razor -->
...
@code {

    ...

    protected void OnMouseOver(MouseEventArgs focusEventArgs)
    {
        ClassName = "highlight";
    }

    protected void OnMouseOut(MouseEventArgs focusEventArgs)
    {
        ClassName = string.Empty;
    }

}
<!-- NoteViewComponent.razor -->
@if (Note != null)
{
    <li class="@ClassName" @onmouseover="@OnMouseOver" @onmouseout="@OnMouseOut">
        ...
    </li>
}
@code {

	...
}
<!-- NoteListingComponent.razor -->
@using System.Diagnostics
@page "/"
...
@code {

	...
    protected void OnSubmitNote(MouseEventArgs mouseEventArgs)
    {
        var stopwatch = new Stopwatch();
        stopwatch.Start();

        while (stopwatch.Elapsed.TotalSeconds < 2)
        {

        }

        Notes.Add(new Note(NewComment));
        NewComment = string.Empty;
    }

    ...
}
<!-- NoteListingComponent.razor -->
<div class="col-6">
    ...
    <button type="submit" @onclick="@(async(e) => await OnSubmitNoteAsync(e))">Submit</button>
</div>
...
@code {

    ...

    protected async Task OnSubmitNoteAsync(MouseEventArgs mouseEventArgs)
    {
        await Task.Delay(new TimeSpan(0, 0, 2));

        Notes.Add(new Note(NewComment));
        NewComment = string.Empty;
    }

    ...
}

Previous PostNext Post

Related code examples