Executing Logo Button Click with Selenium in Python

When I try to click on a logo button after completing some downloads, it doesn't work and throws the following exception:

NoElementException: unable to locate element: {"method":"css selector","selector":"brand.brand-bv"}

The structure of the element is as follows:

<a class="brand brand-bv" href="https://workbench-c4.bazaarvoice.com">
  <span class="visuallyhidden">Bazaarvoice:</span>
</a>

This is my current code snippet:

logo_button = driver.find_element_by_css_selector("brand.brand-bv")                                                      
logo_button.click()

https://i.stack.imgur.com/Y3UqB.png

Answer №1

brand signifies a class, and when utilizing a css_selector, it must have a leading .

driver.find_element_by_css_selector(".brand.brand-bv")

The combination brand.brand-bv refers to an element with the tag brand and the class brand-bv

<brand class="brand-bv"/>

To ensure the element loads properly, you can include some waiting time

logo_button = WebDriverWait(driver, 10).until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, ".brand.brand-bv")))

It is necessary to import these libraries

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

Answer №2

For a more versatile approach, consider using xpath to achieve the same result. Locate the element and store it in a variable "e", then verify its existence before clicking on it. It is also recommended to wait for the element to appear before performing any click actions.

e = driver.find_element_by_xpath("*//a[href='https://workbench-c4.bazaarvoice.com']")

if len(e) > 0
    e[0].click()

The syntax for finding elements by xpath is as follows:

driver.find_element_by_xpath("*//TAGNAME[ATTRIBUTENAME='ATTIBVALUE']")

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

Getting superclasses ready in Python

Currently, I am in the process of learning Python. While working on a project involving creating a class for a colored square, I faced an issue with inheritance. My code works perfectly fine with composition but throws an error when I attempt to implement ...

Running Selenium tests through Jenkins on Ubuntu server 16.04 in headless mode, completely bypassing the need for a graphical user interface

I am currently engaged in Selenium Automation Testing with Jenkins. After successfully setting up Jenkins on my computer, I have been able to run tests as a Maven project without any issues. The Jenkins setup is on a Ubuntu server that operates without a ...

Flask does not provide a direct boolean value for checkboxes

After struggling for a week, I am still lost on where to make changes in my code. I need the checkbox to return a boolean value in my Flask application. Below are snippets of the relevant code: mycode.py import os, sqlite3 from flask import Flask, flash ...

Ways to avoid running any cucumber-jvm scenarios if the initial scenario fails

I have 8 scenarios using cucumber-jvm, with the very first scenario focused on measuring page load time and checking environment availability. If this initial scenario fails, such as due to an unavailable environment or slow loading times, I want all other ...

Implementing a primary key into an already established MySQL table using alembic

Attempting to add a primary key column with an 'id' identifier to an existing MySQL table using alembic. The process involved the following steps... op.add_column('mytable', sa.Column('id', sa.Integer(), nullable=False)) op.a ...

Does the CSS :not() selector function properly when used with distant descendants?

Check out the official documentation for the CSS3 :not() pseudo-class at this link: http://www.w3.org/TR/css3-selectors/#negation Additionally, you may be interested in the proposed enhancement for CSS Selectors Level 4 found here: http://dev.w3.org ...

Is there a way to display and output the data from a file?

I'm currently working on a program that opens a file and displays its contents. I'm running into issues with defining it, I think? Either I get an error saying "path" is not defined or "new_dir" is not defined. Below is the code: import pathlib ...

Is there a way to print a particular value within a matrix using Python, specifically with the help of numpy?

I am facing an issue with a code snippet that is supposed to generate a zero matrix based on a given size (m x n). However, my goal is to extract a specific value from a particular location within that matrix. Despite trying numerous approaches, I have not ...

The Selenium Python module is having difficulty locating the element for the name or email address

Currently, I am attempting to automate the login process on a website using Selenium with Python. Unfortunately, I encountered an error message shown below. Traceback (most recent call last): File "C:\Users\KienThong\Automation\L ...

Who snatched the python requests shib_idp_session cookies from the cookie jar's grasp?

Currently, I am experimenting with leveraging Python (3.9.1) along with requests (2.25.1) to log in using Shibboleth and Duo two-factor authentication (2FA). I possess all the necessary credentials for this process and regularly perform manual logins throu ...

Python - finding characters located between repeated specific characters within a string

Currently, I'm dealing with a large string that contains approximately 270 million lines as shown below: <DOC> <DOC>+BDTag <S> <S>+BSTag --- ---- --- ---- </S> </S>+ESTag <S> <S>+BSTag --- ---- --- ---- ...

Determining the Euclidean distance between row combinations in a pandas dataframe: A guide

I am dealing with a DataFrame that contains information about clusters and their respective variables: import pandas as pd foo = pd.DataFrame({'cluster': [1,2,3], 'var1': [0.3,0.5,1], 'var2&apo ...

Json output is simply displaying the results without any additional formatting

When parsing a JSON output, I am encountering the following issue where it is printing in 'u'' format. How can I make it print only "deleted"? error=change['Errors'] print error Output: [u'DELETED'] Expected output: ...

Error: The script was unable to locate the requested files

Currently, I am working on a Python script that will traverse through all the directories located in the same directory as the script itself, along with their subdirectories and files within those directories that have the ".symlink" suffix. The goal is to ...

Minimizing data entries in a Pandas DataFrame based on index

When working with multiple datafiles, I use the following code to load them: df = pd.concat((pd.read_csv(f[:-4]+'.txt', delimiter='\s+', header=8) for f in files)) The resulting DataFrame looks like this: ...

Eliminate redundant rows by comparing values across all columns with the help of pandas

I am working with a pandas dataframe that consists of various permutations of values: (below is a simplified version, but my actual dataframe has more rows and columns) My objective is to eliminate the rows that have duplicate values not just within indiv ...

How can Python be used to tally instances of duplicate groups?

As a newcomer to python, I am currently grappling with how to count groups of duplicates within a list. For instance, when presented with the following list: ['down', 'down', 'down', 'up', 'right', 'r ...

Implementing Conditional Statements in Date-picker with Selenium WebDriver using Python

As a newcomer to Python/Selenium, I am attempting to construct a statement that will execute an alternative action when the specified element cannot be located. Below is the code snippet in question. pickActiveDay = driver.find_element_by_xpath('//di ...

Failed cron job in Google App Engine Python environments is causing issues

I am encountering an issue with a script running on Google Appengine that is triggered every 20 minutes by the cron.yaml file. The script runs successfully when tested locally on my machine and even when manually accessed online, but fails to complete when ...

Having difficulties in selecting an element on a webpage using xpath without encountering any error messages

Testing a website's form submission page, I encountered an issue with the submit button code: <input id="ctl00_PlaceHolderMain_SubmitButton" class="SubmitButton" type="submit" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackO ...