Tips on how to use Selenium and Java to successfully click on the "logout" link in HTML

Below is the code snippet:

<div id="user-tools">
    Welcome
    <strong>Admin</strong>
    /
    <a href="/">View</a>
    /
    <a href="/admin/password_change">Change password</a>
    /
    </a href="/admin/logout/">Log out</a>
</div>

I am attempting to use this method to click on the "Log out" link, but it isn't working.

driver.findElement(By.xpath("//a[@href='/admin/logout/']")).click();

Any suggestions? Thank you.

Answer №1

If you need to interact with the element displaying the message Log out, you have the option of using any of these Locator Strategies:

  • CssSelector:

    "div#user-tools a[href='/admin/logout/']"
    
  • XPath:

    "//div[@id='user-tools']//a[@href='/admin/logout/'][contains(.,'Log out')]"
    

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

To interact with elements on a webpage, utilize XPath and Selenium in Python to

Typically, I rely on xpath to click on text within web pages. However, in this case, it seems that due to it being a table, the xpath method is not working. I am trying to click on the "SNOW Microsoft 2019-03-26.csv" text, which is unique within the table. ...

Activate a new browser tab with Selenium

Currently, I am utilizing Selenium with PhantomJS in Python to handle a new window. For the purpose of testing, my approach is as follows: from selenium import webdriver driver = webdriver.PhantomJS() driver.get('http://www.google.com.br') han ...

Having trouble finding the search box element within selenium using Python

I am struggling to find the search box on a website and enter values for searching using Selenium in Python. Despite trying different methods, I have been unsuccessful in locating the element. Here are some of the ways I attempted: element = driver.find_e ...

Ways to iterate through elements using selenium

Seeking guidance on utilizing selenium to iterate through multiple div elements on a webpage and retrieve their content The structure of the webpage is as follows: <html> <div data-testid="property-card"> <div data-testi ...

Utilize Python's Selenium webdriver to operate Firefox in the background

Currently, I am involved in a website scraping endeavor utilizing Selenium with Python. One question that has crossed my mind is whether it's feasible to initiate the Firefox browser in the background or launch Firefox on a separate workspace within U ...

Just getting started with the selenium package, experimenting with entering login information

Hello everyone, I need some input to help me troubleshoot an issue I'm having with my code. I am trying to log in to a specific website using Selenium, but I keep encountering this error message: An Exception has occurred: AttributeError 'list ...

Selenium-powered Python Web Scraping

I am attempting to gather product information from Amazon, but I keep encountering the error NoElementException: Message: unable to find element: {"method":"xpath","selector":"//li[@class='a-last']"} (Session info: chrome=102.0.5005.115) The cod ...

Step-by-step guide to generating a fingerprint for Chrome 79 with Selenium

After attempting to run the following code line, it failed to execute properly: option.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36") Despite setting ...

Extract all relevant URLs and Hrefs from the web using web scraping techniques

I am working on a project that involves analyzing multiple dropdowns in a webpage. These dropdowns contain various URLs/hrefs, which I need to extract and print out. After realizing that all the hrefs share a common partial link text, I attempted to gath ...

Waiting for elements to be loaded in Selenium and Python: A step-by-step guide

Imagine I am choosing with the following selector: //img[@data-blabla] If I want to wait for 10 elements to be loaded, not just one, how should this be altered? My assumption is to use the index [9] WebDriverWait(browser, 5).until(EC.presence_of_element_l ...

How to compare values with the same class name in Selenium and programmatically click on the link with the highest value in Python?

I currently have 5 or 6 products listed under an anchor tag, each containing the product name and price within distinct div elements. My goal is to compare the prices of all the products and then click on the link that corresponds to the product with the h ...

How to extract information from a shared notebook on Evernote using Python

Trying to retrieve data from a shared Evernote notebook, such as this one: Initially attempted using Beautiful Soup: url = 'https://www.evernote.com/pub/missrspink/evernoteexamples#st=p&n=56b67555-158e-4d10-96e2-3b2c57ee372c' r = requests.g ...

When trying to retrieve an element, Selenium may throw an exception indicating that there is an

Recently, I've been working on coding a bot that can automatically log in to a website. Right now, I've started with the code for the login process focusing only on collecting the email input field. try: WebDriverWait(bot, delay).unti ...

Using Selenium in Python to extract specific sections of HTML code (excluding text) through web scraping

I am looking to utilize Selenium in order to extract a specific portion of the HTML code from a webpage for a project I am currently working on. My goal is to get the actual code itself, not just the inner text, and then save it either as an external text ...

Steps for configuring input value in readonly state

Below is the image of the text field that requires editing. https://i.stack.imgur.com/vaegA.png During my attempt to edit the text field using the following code: c7 = driver.find_element_by_xpath("/html/body/div[@id='root']/div[@id='wrap ...

What is the best way to make a python script repeat a set number of times continuously?

To repeat a Python script 10 times, I encountered an intentional error that halts the process. This occurs because the script interacts with a website where a random number of questions are generated, up to a maximum of 7. To handle this variability and pr ...

Mastering the art of bypassing the Javascript "onclick" button to extract data from a web page using

I am attempting to scrape data from a website, specifically a table. While I was able to successfully scrape the table, I encountered an issue when trying to scrape other pages. Unfortunately, these pages do not have any href links. When attempting to cli ...

Python script for extracting content from iframes without specified sources

Currently, I am attempting to extract the content within an iFrame with the ID "topic" from the following HTML file: https://i.stack.imgur.com/FustQ.png Even after trying methods like Selenium and Beautiful Soup, I am still unable to access the elements i ...

Navigating through a series of URLs using Selenium

I am encountering difficulties with looping through a list of URLs using selenium. The problem seems to lie within the #Second Part section of my code. Currently, the length of the linklinkfin list is 9, but this number can fluctuate as more URLs are gathe ...

The problem with importing a pre-defined Selenium function in the browser

Initial Set-up Working with selenium, I found myself constantly redefining functions. To streamline the process, I decided to create a separate file for these functions and import them as needed in my work files. A Basic Example Initially, when all fun ...