Using Selenium in Python to retrieve a specific child element by referencing its parent

I am struggling to locate a specific subelement within a list of parent elements, encountering an issue where the full xpath works but breaking it down into parent => child does not yield the correct location (resulting in an error message).

Here are the variations I have tried:

'//div/div[1]/button/div/div/div/div[1]/h2 => successful but does not start from the parent element

'/div/div[1]/button/div/div/div/div[1]/h2' => unsuccessful

'.//div/div[1]/button/div/div/div/div[1]/h2 => unsuccessful

'./div/div[1]/button/div/div/div/div[1]/h2' => unsuccessful

The issue likely lies with the parent element, though the reason remains unclear to me...

Example of one element from the parents list: <selenium.webdriver.remote.webelement.WebElement (session="b01fcfc57a8f3d55ad083363c31fa4c3", element="cf495e96-8be5-425e-b404-b5449f694bab")>

self.driver.get(self.shop_link + page_link)

    WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.XPATH, '/html/body/div/div[1]/section/div/div/div[5]/div[1]/div[2]/div/div/div[1]/button/div/div/div/div[1]/h2')))
    elements = self.driver.find_elements(By.XPATH, '/html/body/div/div[1]/section/div/div/div[5]/div[1]/div[2]/div')

    for element in elements:
        print(element)
        filter_name = element.find_element(By.XPATH, './/button/div/div/div/div[1]/h2')
        print(filter_name.text)

    '/html/body/div/div[1]/section/div/div/div[5]/div[1]/div[2]/div[12]/div/div[1]/button/div/div/div/div[1]/h2'  # full xpath
    # =>
    '/html/body/div/div[1]/section/div/div/div[5]/div[1]/div[2]/div    /div/div[1]/button/div/div/div/div[1]/h2'   # xpath after removing the changing variable

Error Message:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":".//button/div/div/div/div[1]/h2"}

Thank you very much in advance for any assistance!

Answer №1

The code snippet below may not be perfect, but it serves its purpose. In this example, I am choosing the "Canvas" filter from the "Type" dropdown menu.

import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC


options = Options()
options.add_argument("start-maximized")


webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
wait = WebDriverWait(driver, 10)
url = 'https://www.lowes.ca/dept/art-wall-decor-home-decor-furniture-a28?display=100'
driver.get(url)
wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//div[contains(@id,'search-facet')]")))
time.sleep(6)
blocks = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//div[contains(@id,'search-facet')]")))
blocks[0].find_element(By.XPATH, ".//label[contains(@title,'Canvas')]").click()

It's important to master the art of creating efficient locators for your automation scripts.

Answer №2

the first element does not have a subelement found, but the rest are functioning properly,

from clicknium import clicknium as cc

if not cc.chrome.extension.is_installed():
    cc.chrome.extension.install_or_update()
tab = cc.chrome.open("https://www.lowes.ca/dept/art-wall-decor-home-decor-furniture-a28?display=100")
elements = tab.find_elements_by_xpath('/html/body/div/div[1]/section/div/div/div[5]/div[1]/div[2]/div')

for element in elements:
    print(element.get_text())
    filter_name = element.find_element_by_xpath('.//button/div/div/div/div[1]/h2')
    if filter_name != None:
        print(filter_name.get_text())
    else:
        print("none")

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

Is there a way to utilize two instances of a Chrome driver on a single profile using Selenium Webdriver in Node

When writing tests, I prioritize speed and efficiency. One way to achieve this is by ensuring that the user is already authenticated and has loaded data in the local store. import * as webdriver from 'selenium-webdriver'; import * as Chrome from ...

Notification that appears during the process of writing Python code in Visual Studio Code:

I'm a beginner learning Python and using VS Code as my code editor. However, when I write code, I keep seeing a message in a red circle like the one highlighted in the screenshot below. It's starting to get on my nerves. Can someone help me figu ...

Navigating through API replies

I am attempting to iterate through an API response in order to display unique, non-repeating ip-srcs and their corresponding values. I have successfully made the API call in Python and formatted the output as JSON, but I am struggling with filtering the di ...

Discovering the positions of HTML elements rendered with WebKit (or Gecko) technology

I am seeking a way to retrieve the dimensions (coordinates) of all HTML elements on a webpage as they appear in a browser, including their rendered positions. Specifically, I need to obtain values like (top-left, top-right, bottom-left, bottom-right) Afte ...

What is the best way to execute a batch file in the command prompt that will then run a Python script?

Today I'm looking to develop a unique command in the command prompt. Let's say: C:\Users\User> execute Greetings In this case, execute will serve as the distinct command that triggers a batch script to execute a Python file while tr ...

What's causing the no attribute error in Selenium with Python?

This is the error that is displayed in my console: An AttributeError: 'WebDriver' object does not have the attribute 'find_element_by_link_text' Upon examining the source code of the website I am working on, I found that the link I ne ...

Retrieve the outer-HTML of an element when it is clicked

I am working on a project to develop a tool for locating xpath, and I am seeking the most efficient and user-friendly method for allowing the user to select the desired element on a webpage. Ideally, this selection should be made with just a single click, ...

What can be done to ensure that column names in a dataframe do not automatically change after performing a merge or join

I'm encountering an issue in Python 3.8 where my dataframe columns are automatically changing to (column name,) after using join or merge. I've tried using join, merge, and concat but they all result in the same problem :(( Here are some images ...

Selenium in Python encountered an error while trying to access the file for storage reserve marking

Currently, I am utilizing EdgeDriver in conjunction with Selenium and Python 3. To resolve the issue, I manually created the js folder. However, the EdgeDriver window fails to load the page and displays Data;, in the address bar. These issues might be con ...

Discover every item that begins with 'button-'

Currently, I am utilizing Selenium to retrieve all ID elements that begin with "button-". My initial approach involved using regex to locate the "button-" but unfortunately, I encountered an error message stating that TypeError: Object of type 'SRE_Pa ...

Substitute the section of the text that aligns with the given template

Today is my first day diving into Python, so please bear with me as I ask this question The Issue: I need to replace every instance of trim(column_name) with TRIM (TRIM (CHR (09) FROM column_name)) in a file. I've managed to figure out how to search ...

Incorrect boundaries

Below is the code I am using: import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation fig, ax = plt.subplots() xdata, ydata = [], [] ln, = plt.plot([], [], 'ro') def init(): ax.set_xlim(0, 2*np.pi) ...

A guide on choosing options within a select tag using Android WebDriver

Currently, I am in the process of writing test cases for a website specifically designed for android devices. One of the key requirements is to select an option from a dropdown menu on the page. Unfortunately, it appears that the android web driver does no ...

Trouble alert: The module theano could not be found

In my deep learning studies, I often refer to the introduction in tutorials. sudo apt-get install python-numpy python-scipy python-dev python-pip python-nose g++ libopenblas-dev git sudo pip install Theano When running tests for numpy and scipy, everyth ...

An issue with the Selenium webdriver: 'to_capabilities' attribute not found in 'NoneType' object

I've encountered an issue while attempting to run the code snippet below in Python: Cell In[11], line 10 driver = webdriver.Remote(service.service_url,options) File ~/anaconda3/envs/algo/lib/python3.10/site-packages/selenium/webdriver/remote/ ...

Python - Establishing a Connection with AWS Neptune

After setting up a Neptune instance on AWS, I'm facing issues with connecting to it. I followed the steps mentioned in the documentation using my laptop locally. from gremlin_python.structure.graph import Graph from gremlin_python.driver.driver_remot ...

Looking for assistance with printing a vertical list?

Being a complete novice to Python, I have exhausted all other options and turned to this platform for help. Despite spending hours on my study notes and searching the internet, I am unable to grasp what I need to do. The list that I am dealing with is str ...

Issues are arising with Jquery Scripts when running through Selenium in IE 9, resulting in the error message SCRIPT5009: '$' is undefined. However, these scripts are functioning correctly when executed directly in the web browser

While using Selenium, the code below is causing issues as it is not functioning properly. An error SCRIPT5009: '$' is undefined is being thrown in IE 9. However, if the code is executed in a web browser console after removing the "\" sign, i ...

Tips on personalizing the BROWSERSTACK_BUILD_NAME within Jenkins

Currently, I am in the process of incorporating BrowserStack into my Selenium Python framework and utilizing Jenkins for running tests. However, I am encountering difficulties when trying to personalize the build name on the BrowserStack dashboard. ...

How to read files string-by-string and perform operations in NodeJS using fs.readFileSync?

Apologies for the simple question, How can I read from a file in NodeJS, string by string, extract values such as URLs, and perform operations on each string? var contents = fs.readFileSync('test.txt', 'utf8'); What should I do next? ...