Locating an element with dynamic ID using Selenium and Python - python

I need to find an element by ID "start-ads", but the numbers at the end change every time. Is it possible to search for an element not by the whole ID, but only by its part?
Full element id: <div id="start-ads-202308">

To identify the following element:
<div id="start-ads-202308">
considering the static part of the value of id attribute you can use either of the following Locator Strategies:
Using css_selector:
element = driver.find_element(By.CSS_SELECTOR, "div[id^='start-ads-']")
where ^ denotes starts-with
Using xpath:
element = driver.find_element(By.XPATH, "//div[starts-with(#id, 'start-ads-')]")

You can use find_element_by_css_selector and use a CSS selector that matches using a prefix
driver.find_element_by_css_selector("div[id^='start-ads-']")

Related

No element found by XPATH

I am trying to retrieve the text of a specific element in a table with the following XPATH:
/html/body/form[2]/table/tbody/tr/td/table/tbody/tr[2]/td[7]/input
using
driver.maximize_window() # For maximizing window
driver.implicitly_wait(3) # gives an implicit wait for 20 seconds
driver.find_element(By.XPATH, value = "/html/body/form[2]/table/tbody/tr/td/table/tbody/tr[2]/td[7]/input").text()
but I get the following error:
NoSuchElementException: Message: Unable to locate element: /html/body/form[2]/table/tbody/tr/td/table/tbody/tr[2]/td[7]/input
I have also tried accessing the element by CSS selector and value, without success.
Unfortunately the link is secured so I cannot share it but here is a screenshot of the element
Instead of your absolute XPath, try relative XPath. Below is the expression:
//input[#name='AdjIncrementAmount_1']
from the screenshot, I am not really sure if the value of attribute name (after the text Amount) has one _ or two __. If the above with(one _) doesn't work, try with 2 as below:
//input[#name='AdjIncrementAmount__1']
To print the text $36,400.00 you can use either of the following locator strategies:
Using css_selector:
print(driver.find_element(By.CSS_SELECTOR, "td > input[name='AdjIncrementAmount__1']").get_attribute("value"))
Using xpath:
print(driver.find_element(By.XPATH, "//td/input[#name='AdjIncrementAmount__1']").get_attribute("value"))
Note : You have to add the following imports :
from selenium.webdriver.common.by import By

How to get the inner text from an <a> tag

I want to select the Remember Sports part of this line
Remember Sports
I can get the href but how do I get the actual text?
This is what I tried, when I tried to pass another argument into get_attribute or leave it blank then it doesn't work.
browser = webdriver.Chrome('c:\\Users\\16308\\Documents\\VSCPython\chromedriver',chrome_options=chrome_options)
url = 'https://www.allmusic.com/album/sunchokes-mw0003322304'
browser.get(url)
stages = browser.find_element_by_class_name('album-artist')
artist_link = stages.find_element_by_css_selector('a').get_attribute('href')
artist_link_text = stages.find_element_by_css_selector('a')
browser.get(artist_link)
print(artist_link_text)
Thanks for any help.
You can try to use .get_attribute("innerHTML"), if it was an input you could also use 'value' instead of innerHTML.
EDIT:
There is another very similar question: Use Python Selenium to get span text
Select your element using xpath or css then use get text method to get the text inside:
Text=stages.find_element_by_css_selector('a').text
Or
text=driver.findElement(By.className("Class Locator").getText( );
The text Remember Sports is within the following element:
<h2 class="album-artist">
<span>
Remember Sports
</span>
</h2>
Solution
To extract the text you can use either of the following locator strategies:
Using CSS_SELECTOR and get_attribute("innerHTML"):
print(driver.find_element(By.CSS_SELECTOR, "h2.album-artist a[href^='https://www.allmusic.com/artist']").get_attribute("innerHTML"))
Using XPATH and text attribute:
print(driver.find_element(By.XPATH, "//h2[#class='album-artist']//a[#href='https://www.allmusic.com/artist/remember-sports-mn0003731048']").text)

unique xpath for element span inside div inside button selenium python

Xpath
I'm looking for Unique Xpath for this element (Select)
in google language settings
https://myaccount.google.com/language?hl=en
already have this xpath but I need something more accurate and unique
WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH,
"//*[#id='yDmH0d']/div[11]/div/div[2]/div[3]/div[2]/button"))).click()
If you pay attention to the HTMLDOM :
You can construct a xpath based on attribute name only. You do not need their values nor their text.
//div[#data-is-touch-wrapper]/button[#data-id and #aria-label]
represent two matching nodes.
(//div[#data-is-touch-wrapper]/button[#data-id and #aria-label])[2]
should get the job done.
I would try to copy the X-Path from the Browser as discribed here:
Mozilla Help
My result would be: //*[#id="lang-selector"]

Is there a way to get the value within a <div> using selenium in python?

I'm new to python and webscraping so I'm not sure what the name of the value inbetween the <div>'s in an element is called. Sorry for not being able to specify.
<div class="syllable">value</div>
Is there a way to have the value inbetween the <div>'s get assigned to a string variable in python using selenium using XPath?
For example, the "value" in the element would be a string and it would print out:
value
I'm new to python and selenium so I can't figure it out.
To print out the text of the element.
elem=driver.find_element_by_class_name("syllable")
print(elem.text)
xpath:
elem=driver.find_element_by_xpath("//div[#class='syllable']/text()")
print(elem)
it is called html innerText
you can retrieve this value using text in selenium , or get_attribute.
This returns the rendered text (means displayed text)
elem=driver.find_element_by_class_name("syllable")
print(elem.text)
This return the text with out checking the style attribute meaning returns value even if its not displayed in UI
elem=driver.find_element_by_class_name("syllable")
print(elem.get_attribute("textContent")
you can find elem using this text also:
// partial match
elem=driver.find_element_by_xpath("//div[contains(text(),'value')])
print(elem.text)
// exact match
elem=driver.find_element_by_xpath("//div[text()='value')])
print(elem.text)
// exact match of the elements text if there is any child element like span it won't return the element
elem=driver.find_element_by_xpath("//div[.='value')])
print(elem.text)
Also note:
Other things you could read about outerHTML , innerHTML
To print the text value you can use either of the following Locator Strategies:
Using class_name and get_attribute("textContent"):
print(driver.find_element_by_class_name("syllable").get_attribute("textContent"))
Using css_selector and get_attribute("innerHTML"):
print(driver.find_element_by_css_selector("div.syllable").get_attribute("innerHTML"))
Using xpath and text attribute:
print(driver.find_element_by_xpath("//div[#class='syllable']").text)
Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CLASS_NAME and get_attribute("textContent"):
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, "syllable"))).get_attribute("textContent"))
Using CSS_SELECTOR and text attribute:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.syllable"))).text)
Using XPATH and get_attribute():
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='syllable']"))).get_attribute("innerHTML"))
Console Output:
value
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python
References
Link to useful documentation:
get_attribute() method Gets the given attribute or property of the element.
text attribute returns The text of the element.
Difference between text and innerHTML using Selenium

How to search for multiple attributes selenium

HTML:
<button type = "submit" class = "car">
How should I search for this button in selenium python using both the attributes type and class in find_element_by_xpath()
To locate the element using both the attributes class and type you can club up them in a single locator and you can use either of the following Locator Strategies:
Using css_selector:
element = driver.find_element_by_css_selector("button.car[type='submit']")
Using xpath:
element = driver.find_element_by_xpath("//button[#class='car' and #type='submit']")
References
You can find a couple of relevant detailed discussions in:
How to identify an element through classname even though there are multiple elements with the same classnames using Selenium and Python
Try to use this XPATH
'//button[#type="submit" and #class="car"]'

Categories