i'm trying to find a specific sub element for a list of parent, but i have a problem: the full xpath works, but when i try to separate it into parent => child, it can't find the correct location(that's the error message)
I tried using:
'//div/div[1]/button/div/div/div/div[1]/h2 => it works but don't start from the parent so it's not what i want
'/div/div[1]/button/div/div/div/div[1]/h2' => doesn't work
'.//div/div[1]/button/div/div/div/div[1]/h2 => doesn't work
'./div/div[1]/button/div/div/div/div[1]/h2 => doesn't work
It's probably due to the parent element, but i don't understand why .. exemple of one element of 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"}
Thanks a lot in advance for your help !
CodePudding user response:
The following code is not ideal, just to show how it works. But it works.
Here I'm selecting "Canvas" option from "Type" filters.
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()
You need to learn how to create effective locators
CodePudding user response:
it is failed to find subelement for the first element, but the remaining work well,
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")