-
Notifications
You must be signed in to change notification settings - Fork 0
Generate XPath expressions of elements inside selenium element
mluis7 edited this page Dec 23, 2024
·
4 revisions
Given a selenium element, generate XPath expressions of all button elements descendants of that element.
In other words: programmatically generate xpath expressions to be used by selenium.
import time
from lxml import html, etree
from xml2xpath import xml2xpath
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument("window-size=2880x1620")
options.add_argument("--headless")
options.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")
options.set_capability("pageLoadStrategy", "normal")
options.add_argument("--enable-javascript")
prefs = {"profile.managed_default_content_settings.images": 2, "permissions.default.stylesheet": 2}
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(options=options)
driver.implicitly_wait(30)
url = "https://stackoverflow.com/questions/68251508/is-there-a-way-to-debug-why-a-chrome-request-was-queued/68734995#68734995"
driver.get(url)
try:
tree_start = '//div[@id="answer-68734995"]'
answer = driver.find_element("xpath", tree_start).get_attribute("outerHTML")
# parse answer html with lxml.html
doc = html.fromstring(answer)
# build all XPath expressions for button elements with xml2xpath
tree, nsmap, xmap = xml2xpath.parse(file=None,itree=doc.getroottree(), xpath_base=f"{tree_start}//button")
#xml2xpath.print_xpaths(xmap, mode="all")
for x in xmap.values():
button = tree.xpath(x[0])[0]
print(f"Label: '{button.text.strip()}', xpath: {x[0]}")
# click it?
# button_sel = driver.find_element("xpath", x[0])
# button_sel.click()
finally:
driver.quit()