Python Selenium - Trouble clicking button element without redirecting to desired link

I am currently conducting a test on a web user interface using Selenium in Python. In one of the test cases, there is a button that should redirect to another page when clicked.

However, despite executing the code without any errors, the page does not redirect as expected. I have confirmed that the button is being clicked properly, as indicated by the animation and cursor changes.

I have attempted clicking the button directly as well as using the Javascript click method.

`sign_in = driver.find_element(By.CSS_SELECTOR, "button[class='btn btn-primary-blue']")
 sign_in.click()
 driver.execute_script("arguments[0].click();", sign_in)`

Here is the element in question:

`<div class="editorial-hero-banner--footer pb-35 pb-sm-0"><div class="SSOLogin__Container"><button type="button" class="btn btn-primary-blue">Sign in / Register</button></div></div>
    <div class="SSOLogin__Container"><button type="button" class="btn btn-primary-blue">Sign in / Register</button></div>
        <button type="button" class="btn btn-primary-blue">Sign in / Register</button>`

Answer №1

Consider this HTML snippet:

<div class="editorial-hero-banner--footer pb-35 pb-sm-0">
    <div class="SSOLogin__Container">
        <button type="button" class="btn btn-primary-blue">Sign in / Register</button>
    </div>
</div>

The element labeled as Sign in / Register is a dynamic component.


Solution

In order to interact with the interactive item, you must implement the use of WebDriverWait for the purpose of checking if the element is clickable using element_to_be_clickable(). You may utilize either of these locator strategies:

  • Utilizing CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.SSOLogin__Container > button.btn.btn-primary-blue"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-primary-blue'][starts-with(., 'Sign in') and contains(., 'Register')]"))).click()
    
  • Note: The following imports need to be included :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

Alternate Approach

Alternatively, the executeScript() method from JavascriptExecutor can also be utilized in the following manner:

  • Using CSS_SELECTOR:

    driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.SSOLogin__Container > button.btn.btn-primary-blue"))))
    
  • Using XPATH:

    driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-primary-blue'][starts-with(., 'Sign in') and contains(., 'Register')]"))))
    

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to Generate a Collection of Textfields in Angular 4

My challenge involves working with an array object that receives input from a textfield. I am looking to create a clean design where only one textfield is initially displayed, along with a '+' button next to it. When the user enters information i ...

Having Trouble Accessing Website Using Python Script on Firefox

After attempting to launch a website in Firefox using Selenium Python, the browser opens but nothing happens beyond that. An error message was encountered during this process: bash-3.1$ C:/Users/user/AppData/Local/Programs/Python/Python35-32/python.exe d: ...

Python Code for Image Upload

Currently, I am attempting to change my avatar using code in Python on a Linux operating system: photo = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="ButtonChangePhoto"]'))) photo.send_keys('/home/nataliya/Desktop/puppy.jp ...

How to programmatically interact with a dropdown menu without a select element in Python using Selenium

Hey there, I'm currently practicing my skills with Selenium on a practice forum. If you're interested in checking it out, here's the link: click here When you visit the page and inspect the dropdown menu for state and city, you'll noti ...

Submitting data on a web form using Python when the button only has a type and value can be achieved by utilizing specific

I have a specific URL where I need to input some product numbers in order to retrieve the current URL. The form structure is as follows: <form method="get" action="/search" accept-charset="utf-8"> <p> <span class="search-bar-input-wr ...

Selenium webdriver offers the capability to retrieve null values through the Webelement

How can we use Selenium WebDriver with a keyword-driven framework to recognize and open verification emails in Gmail when the registration is done from a different domain? I have created two methods - one containing all the keywords with operations, and a ...

Error: Attempting to send a POST request to a Flask application from another Python script without being in a request context

Attempting to send a POST request to a Flask app from another Python script is causing a RuntimeError with the message: "Working outside of request context". The Flask app (app.py) looks like this: import datetime import numpy as np import pandas as pd fr ...

Trigger immediate matplotlib figure refresh

I am working with a scatter plot in matplotlib and have implemented an onpick() action on the points. When a point is selected, I perform a computation that takes approximately 30 seconds. To provide immediate feedback to the user, I would like to draw a r ...

Utilize protractor to access the window object or browser scope in your test scripts

While conducting tests with protractor, I have encountered difficulty in accessing the JS 'window' object. I attempted to add a script tag in my HTML file containing something like var a = window.location; However, when I tried using expect(a) ...

Manipulate DataFrame in Python using masks to generate a fresh DataFrame

I have a DataFrame that includes columns for date, price, MA1, MA2, and MA3. After filtering the data based on a specific condition, I get a subset of rows where MA1, MA2, and MA3 are equal. date price MA1 MA2 MA3 date1 price1 11 11 11 date4 pri ...

Configuring Dash application testing with webdriver-manager by setting the local path for Chromedriver

I am currently exploring testing Dash applications using the method outlined in this guide: However, I have encountered an issue with specifying the path of Chromedriver for the webdriver-manager used in dash testing. Below is a snippet of what I attempt ...

Is there a way to incorporate circles into a shape with Python?

My current project involves creating a website where users can upload a shape and choose the radius of circles to fill that shape without intersecting at any point. The goal is for the code to position as many circles (with the chosen radius) as possible w ...

What is the best way to retrieve a single object from a ManyToOne table without using a queryset?

Displaying Table Images: id| image | item_id 15|some/path| 45 16|some/path| 45 17|some/path| 45 Initial Attempt: recommended_items = Item.objects.all().filter(rating__gte=9) // Selecting items based on rating for item in recommended_items : ...

Creating a weekly year pivot table in pandas

I'm a beginner with pandas and I need to create pivot tables for each week using data from a csv file that contains records spanning multiple years. I am having trouble figuring out the best way to filter the dataframe by week. Currently, I am doing ...

Update in Jenkins: Alterations to Slave User Access

My Jenkins master is connected to a MAC slave through Java WebStart since SSH is not working. An administrator user named 'qalab' is logged into the machine, which never sleeps. The system information of the node is displayed in the image provid ...

The regex for symmetric start and end patterns is not functioning properly

Python 3 retrieve a string from the file: with open(filepath, "r", encoding="utf-8") as f: content_string = f.read() The content appears like this: --- section-1-line-1 section-1-line-2 section-1-line-3 --- section-2-line-1 sectio ...

What is the best way to hold off until Vue has completed its processing?

When running Selenium tests on a webpage, I often encounter reliability issues because the page is still processing "Vue" when the code begins searching for elements. It's essential to wait until Vue finishes loading before proceeding further. Is the ...

A guide on selecting a video with Selenium

I'm a newcomer to the world of coding with Python and its libraries, as well as being new to Stack Overflow. I apologize if I seem unfamiliar with some things. Now, onto my question. I am attempting to write a script that can automate playing a video ...

Issue with missing modules in cx_Freeze

Looking for some assistance with Python and cx_Freeze as I am new to it. Please guide me on how to make it work by running the command: python setup.py build Encountering an error with missing modules: ? System imported from serial.serialcli ? TERMIOS ...

Is there a way to utilize BackgroundScheduler for updating a variable within a Flask route?

Can you help me figure out how to make the current_time variable update each time the webpage is reloaded in Flask? from flask import Flask, render_template app=Flask(__name__) import time from datetime import datetime from apscheduler.schedulers.backgro ...