Python Selenium, checking if element is present. and if it was present I want it to return a boolean value of TRUE
Here is the HTML Code:
<td data-id="329083" data-property="status" xe-field="status" data-content="Status" style="width: 8%; display: none;" title=" FULL: 0 of 20 seats remain."><div > <span >FULL</span>: 0 of 20 seats remain.</div></td>
I need to check whenever the title changes to " 1 of 20 seats remain." and return a boolean value of TRUE
idk if this helps but this code for checking if someone drops the course from my univercity, i'll create a loop that will keep refreshing the page until someone drops a course so i can register.
Sorry if my english is bad
Thanks in advance
I tried these two different methods to check if the title changed:
1st code:
status = driver.find_element(By.XPATH,"/html/body/main/div[3]/div/div[2]/div/div[1]/div/div[2]/div[3]/div[2]/div[1]/div[1]/div[1]/div/table/tbody/tr/td[11]").text
status.get_attribute(" FULL: 0 of 20 seats remain.")
if status == "FULL:0of20seatsremain.":
print("Seats Not Avaliable")
else:
print("Error")
2nd Code:
title = " FULL: 0 of 20 seats remain."
courseStat = driver.find_element(By.CSS_SELECTOR("[title^='" title "']")).text
Output for 1st Code:
Exception has occurred: AttributeError
'str' object has no attribute 'get_attribute'
status.get_attribute(" FULL: 0 of 20 seats remain.")
Output for 2st Code:
Exception has occurred: TypeError
'str' object is not callable
courseStat = driver.find_element(By.CSS_SELECTOR("[title^='" title "']")).text
I can't share the link because it's the portal for our university, which u will have to log in to view the page source or inspect elements, Here is a screenshot of the HTML code enter image description here
CodePudding user response:
The element attribute containing desired data is title
.
So, the first code can be fixed as following:
status = driver.find_element(By.XPATH,"/html/body/main/div[3]/div/div[2]/div/div[1]/div/div[2]/div[3]/div[2]/div[1]/div[1]/div[1]/div/table/tbody/tr/td[11]").text
status.get_attribute("title")
if "FULL" in status:
print("No seats available")
else:
print("Available seat found")
While you have to improve the locator there.
To correct the second approach we need to see that web page to define a correct locator for that element.