Having trouble locating an element with Selenium

Being new to Selenium and HTML, I have been working on testing a website using Selenium WebDriver. However, the driver is unable to locate an element.

Here's my code:

browser = webdriver.Chrome()
wait = WebDriverWait(browser, 10)
data = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#search_result_former > div.re-content.search-mode-content > div.list-container > ul > li:nth-child(1) > div > div.item-footer > div > a:nth-child(1)')))

The HTML snippet is as follows:

<div class="btn-group clear">
                        <a href="javascript:;" class="btn btn-operation" role="detail">Detailed View</a>
                        <a href="javascript:;" class="btn btn-operation" role="lawState" an="CN201820052763" pn="CN207117855U">Legal Status</a>
                        <a href="javascript:;" class="btn btn-operation" role="proposor" _name="信阳农林学院;" _address=" 河南省信阳市羊山新区新24大街信阳农林学院;" _zipcode="464000;" _country="">Applicant</a>
                        <a href="javascript:;" role="addAnalysis" class="btn btn-operation">+ Analysis Library</a>
                        <a href="javascript:;" role="favorite" class="btn btn-operation">Bookmark</a>
                        <a href="javascript:;" role="translate" _id="CN201820052763.420180316XX" class="btn btn-operation btn-translate">Translate</a>
                        </div>

Upon running the code, I encounter the following error message:

*raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:* 

Interestingly, I am able to visually confirm that the element has indeed finished loading. I attempted using XPATH, but without success.

Answer №1

Based on the HTML you provided, you can utilize the code below:

  • Using LINK_TEXT :

    wait = WebDriverWait(browser, 10)
    data = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "详览")))
    
  • Using CSS_SELECTOR :

    wait = WebDriverWait(browser, 10)
    data = wait.until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn btn-operation' and contains(.,'详览')]")))
    
  • Using CSS_SELECTOR :

    wait = WebDriverWait(browser, 10)
    data = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-operation[role='detail']")))
    

Answer №2

To ensure a successful click on an element that may be obscured by another element, leverage the JavaScript executor:

driver = webdriver.Firefox()

driver.get("http://stackoverflow.com/questions/7794087/running-javascript-in-selenium-using-python")
driver.execute_script("document.getElementsByClassName('comment-user')[0].click()") 

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

Logging DOM elements with Electron's webview preload feature

Within my Electron program, I am utilizing a webview in my index.html file. The webview is equipped with a preloader, and my goal is to manipulate the DOM of the webview specifically, not just the index.html file. In my preloader code snippet below, the c ...

Exploring the word boundary between ASCII and Unicode characters in Python 3

Python Code Snippet: import re k = "X" s = "X测试一Q测试二XQ测试三" print(re.split((r"\b" + k + r"\b"), s)) Output: ['X测试一Q测试二XQ测试三'] Expected Output: ['', & ...

Sorting a matrix by a single row in Python

Consider this matrix: [[0. 1. 0. 0. 0.] [1. 0. 0. 0. 0.] [0. 0. 0. 1. 0.] [0. 0. 0. 0. 1.] [1. 0. 3. 4. 4.]] I need to rearrange it based on the values of the last row while keeping columns intact like so: [[1. 0. 0. 0. 0.] [0. 1. 0. 0. 0.] [0. 0. 0. 1. ...

Unable to retrieve the text enclosed between the:: before and after the:: marker

I attempted this using the XPATH finder in Chrome, and it highlighted the element. However, when running my Selenium script, I received the following error: Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: ...

Importing a Javascript file into an Angular project

I am currently working on an Angular application where routes are redirected to specific HTML pages. However, I am unsure about how to include related JavaScript files along with this redirection process. For instance, when a user clicks on a link that lo ...

Experiencing difficulties with extracting information from mySQL for my web development assignment

I am currently working on a personal project to enhance my skills in web and full-stack development by recreating the NYT Connections game. As part of this project, I have developed a database.js file using node.js which successfully connects to a local da ...

How can you display a border around a <td> element in an HTML table only when it contains data, using jQuery or JavaScript?

My HTML table consists of the following structure: <table class="table table-bordered"> <thead> <tr> <th>Tag</th> <th>Time Code</th> </tr> </thea ...

Which is the best framework to transform a NextJS website into a desktop application: Electron, React-Native, or React-

After recently mastering ReactJS, I am currently utilizing it to create an application with NextJS. My goal is to make this application available as a desktop version as well. I have been introduced to Electron, React-Native, and React-Desktop, but I am u ...

selenium.common.exceptions.TimeoutException: Message: Encountered an issue while attempting to display the titles of shows

Encountered the error shown above when executing this brief script: from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_condition ...

Updating website content dynamically using Javascript and JSON-encoded data

My programming code seems to be acting oddly. I have structured my data in a JSON object as follows: injectJson = { "title": "Champion Challenge Questions", "rules": [ { "idChrono": "chrono-minute", "text": "Top is missing!", ...

Troubleshooting a JavaScript Script Issue in a NextJs Class

I have been working on my website and decided to incorporate an FAQ page. I used a template for the FAQ section and tried to implement Javascript in my NextJs project, but unfortunately, it's not functioning as expected. var faq = document.getEle ...

Can I construct an html table-esque layout employing fabric js?

https://i.stack.imgur.com/Zwkj3.jpg I'm in the process of developing a schema builder inspired by vertabelo. To achieve this, I've opted to utilize fabric.js for its interactive features. My main challenge lies in creating an HTML table-like str ...

Selenium encounters difficulty when attempting to navigate further down the page

I am currently using Selenium for web scraping from this website. The site employs animations to reveal sections as you scroll down. My goal is to reach the footer, wait for the animation to complete, and then extract the data I need. However, I suspect t ...

What is the best way to display a Bootstrap alert above all other elements on the page?

I need help with adjusting the placement of my bootstrap alert. Currently, when the input box value is not valid and the button is clicked, the alert shows up below the input box. I would like it to appear in the middle of the page, on top of the text box. ...

Unexpected outcome when converting a while loop to a .forEach statement

Exploring a practical demonstration of sorting an array of objects based on multiple properties, the code implementation involves a while loop (refer to students1 in the snippet below). I attempted to streamline this process by using .forEach, but encounte ...

The vertexUv in three.js is currently undefined and needs to be

I'm currently facing an issue while trying to combine multiple meshes, one of which is created by inputting the vertices coordinates. This specific mesh is causing the following error: THREE.DirectGeometry.fromGeometry(): Undefined vertexUv 256 W ...

Change web page in JavaScript using post data

Is there a method to utilize JavaScript for navigating to a new URL while including POST parameters? I am aware that with GET requests, you can simply add a parameter string to the URL using window.location.replace(). Is there a way to achieve this with ...

Tips for removing information from a nested array that aligns with an ID in a separate array

Can anyone assist me with removing a nested array that matches the id of the main array? Here is arrayOne: [ { "id": "1", "role": [ "pos_cashier_1", "pos_manager" ...

How to center items within a Toolbar using React's material-ui

I need help with implementing a toolbar on a page that contains three ToolbarGroup components: <Toolbar> <ToolbarGroup firstChild={true} float="left"> {prevButton} </ToolbarGro ...

The Navbar is throwing a TypeError because it is unable to retrieve the property 'classList' of null

I am currently experimenting with building a navbar in HTML that has the capability to dynamically switch pages without changing links using href. The method I'm employing to achieve this involves adding a classList of is-active to the div elements wi ...