Currently, I am trying to take this line of HTML and create an XPATH that can be converted to text and stored as a variable.
<span aria-hidden="true" style="height: auto;">Champion unisex adult Ameritage Dad Adjustable Cap Headband, Medium Black, One Size US</span>
Right now I am using this (By.XPATH, "//span[contains(text(),'Champion unisex')]/text()").text()
And I have also used (By.XPATH, "//span[contains(text(),'Champion unisex')]")
But it isn't converting the XPATH to text. If anyone could help me better understand what would be the best way to create this XPATH and how to convert it to text within Selenium I would be very grateful.
CodePudding user response:
Are you trying to get "Champion unisex adult Ameritage Dad Adjustable Cap Headband, Medium Black, One Size US" as a string to store in a variable? Or are you trying to get the xpath of the element as a varible?
For the first option you just need: VaribleName = driver.find_element_by_xpath("XPATH_HERE").text
For the second you can just right click the element in the developer window and hover over copy then copy xpath.
CodePudding user response:
If you want to get the text and store it in a variable that will wait until it detects the xpath for a maximum of 30 seconds, try this:
hat_verification_links = WebDriverWait(driver, 30).until(EC.presence_of_element_located(
((By.XPATH, '//span[contains(text(),"Champion unisex")]')))).text
try to print it to check if selenium detects it:
print(hat_verification_links)
If you meant you want to make xpath as a variable then get the text:
xpath_value = '//span[contains(text(),"Champion unisex")]'
hat_verification_links = WebDriverWait(driver, 30).until(EC.presence_of_element_located(
((By.XPATH, xpath_value)))).text
print(hat_verification_links)