Fetching text from a link using Selenium in Python

How can I extract the text "Home"?

<li class="active"><a href="#">Home</a></li>

I attempted to use the following code:

element = self.browser.find_element_by_class_name('active')
print element.find_element_by_tag_name('a').text

However, it is returning an empty string. Can someone please help me figure out what I am missing?

Answer №1

Implement this using Cascading Style Sheets -

.find_element_by_css_selector("li.active a")
, or with xpath
.find_element_by_xpath("//li[@class='active']/a")

Here is the finalized version:

element = self.browser.find_element_by_css_selector("li.active a")
print element.text

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

Unable to retrieve information from Excel spreadsheet during automated testing

Kindly review the following code for any necessary corrections: package com.framework; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.time.Duration; import org.apache.poi.ss.usermodel.Sheet; import org.apache. ...

Exploring the Beauty of Selenium and Python by Navigating Links

On a webpage, there are multiple links displayed randomly. The code I've written is set to open the first link in a new tab and perform a function. However, I'm unsure how to handle this if there are more than one link. Is there a way to iterate ...

"Discover the step-by-step process for navigating to the following webpage using web

Currently, I am delving into the world of web scraping and trying to figure out how to navigate through multiple pages on a website until reaching the last page. My goal is to extract data from each page, compile it, and save it in a CSV file. URL = " ...

What could be causing my wait method to not actually wait for the full 30 seconds?

Why is my wait method not waiting for 30 seconds? I want my method to have a timeout of 30 seconds if it cannot find the element. Currently, it just returns true or false immediately without any timeout. Any suggestions on how to fix this? public boo ...

Inconsistency in the functionality of Selenium's clear() command within a loop

The Selenium clear() command may only work once per several iterations in the best-case scenario. In other situations, clear() might not fully clear the field and new send_keys will be appended to the existing text. However, outside of a loop, the same com ...

Learn a method to submit a form in a new tab by utilizing the execute_script function

browser.get('https://www.testsite.com') browser.execute_script("arguments[0].click(), 'new_tab'", browser.find_element_by_xpath('//*[@id="submitButton"]')) This piece of code triggers a new tab to open on specific websites. H ...

Is there a resource that provides a practical example of Java combined with Selenium and PageObject implementation?

While I have some basic experience with Selenium and Java, I am now seeking to delve deeper into the Page Object Pattern. However, I am having difficulty grasping how to effectively implement it in a real project. Despite my efforts to find straightforward ...

Having trouble with clicking a button inside a frame using VBA with Selenium and ChromeDriver

I'm currently facing a challenge related to clicking on a button using Selenium and ChromeDriver in Excel-VBA. Although I have successfully used the same code on many other websites, I am encountering an issue with a specific website. I have tried va ...

Populating date field using Selenium WebDriver

Currently, I am practicing filling out a registration form using Selenium to get more acquainted with this library. The specific registration form can be found on the following website: My Current Approach To start off, my code looks like this: from sel ...

When attempting to click on an element using watir-webdriver, the process failed with the error message "Runtime.evaluate threw exception: Error: element is not attached to

Explanation of HTML Attributes <li class="filter-categ"> <ul class="l-h-list"> <li id="filter__entityBased" class="item filter_value" filter-name="entityBased" search- name="zone/concept/store_based">Zone/Concept/Store base ...

Automating IE with Selenium in a scheduled task using RC

Looking to automate a task with Selenium-RC without the need for the user to be logged in. The ideal scenario involves running Selenium as a scheduled task, launching IE and executing the script seamlessly. Is it possible to simulate user interactions wit ...

I'm facing an issue with my Selenium bot not being able to log into my Gmail account

Currently, I am working on a Python bot that signs me into Gmail. However, every time the bot inputs my information, it gives an error message stating that the browser is not secure. Here is the screenshot of the error I have attempted to troubleshoot th ...

Timeouts in Selenium C#, specifically with WebDriverWait

Utilizing Visual Studio 2013 and C#, I developed a console application to carry out testing on my website with the help of Selenium. This is the code snippet: public static void Main(string[] args) { IWebDriver driver = new FirefoxDriver( ...

Using Python with Selenium to find and interact with a specific <li> element within a <ul> by clicking the element that is inside an <a> tag

Having some trouble here, trying to click on a specific list item in a form. The selection is based on the text/title within the anchor tag (a) inside. I've tried a few things but nothing seems to work. I'm eager to understand where I'm goi ...

Guide on utilizing the print feature in C# Selenium with Chrome driver

On my website, there is a print option that I want to activate. <a href="https://crim.brib.pk/RenderReport_Version2.aspx??Product_Id=MjgwMQ%3d%3d-J2XNpfmNU8Q%3d&amp;RUID=MTEwMDI0NTAwMTU%3d-rBg0P40kyho%3d&amp;FinalResult=T#" onclick="AlertandPr ...

Automating testing with JavaScript and Selenium WebDriver

Can testing be automated using the combination of JavaScript and Selenium? I am not familiar with Java, Python, or C#, but I do have expertise in Front-End development. Has anyone attempted this before? Is it challenging to implement? Are there any recom ...

Verifying the status of a checkbox label in Java using Selenium

I am currently working on an automation project using Selenium and I'm struggling to figure out how to check the status of a checkbox input. Has anyone else encountered this issue before? Here is a sample code snippet in JavaScript with jQuery: $("i ...

The process of removing entered strings from multiple text boxes using Selenium web driver

Struggling with clearing multiple textboxes in a registration form? I encountered an issue where keys sent from the 2nd column of an Excel sheet were being appended to existing strings. While individual clearing with findElement(By.id("xyz")).clear is poss ...

What is the best way to extract text from a div element?

I'm attempting to extract text from a div and save it in a text document using Selenium with C#. I'm facing an issue where I can't retrieve the text from the div and store it in a variable. <div data-tid="messageContent" dir="auto">&l ...

Encountering a problem when executing the mobile automation script using Appium

Encountering an issue while running my automation script with Appium. I'm executing a mobile automation script on a Windows Desktop machine with the following software setup: Software Set-Up: 1. Android Studio 2. Appium 3. Mobile/Tablet connected ...