SebastianC
SebastianC

Reputation: 1

Getting javascript variable value while Selenium Python

rwdata = driver.find_elements_by_xpath(
    "/html/body/div[2]/div/div/div/form[2]/div[2]/table/tbody")

This is a table, with the value of r with type text/javascript

for r in rwdata:
    print(r.text)
    if r.text != "Booked":
        print("Aceptado")

When I print R it has the following values: Booked Booked Booked 9:00 AM Booked Booked Booked 2:00 Am Booked

¿How can I make this comparison, because so far it never enters the if?

        <td scope="row" headers="2/8/2021" class="timetable-cells">

             <div class="cellcontainer" style="height:120px">
                                    

                    <div data-function="timeTableCell"
                         data-sectionid="153"
                         data-servicetypeid="862"
                         data-fromdatetime="2/8/2021 9:00:00 AM"
                         class="pointer timecell text-center "
                         style="top: 0px; height:118px; background-color: #FF0000;color:#000000;"
                         aria-label="Booked"
                         
                         role="row">
                        <label for="ReservedDateTime_2/8/2021 9:00:00 AM">
                            Booked
                        </label>
                    </div>

       

Upvotes: 0

Views: 87

Answers (1)

DMart
DMart

Reputation: 2461

You're never entering the if because you're only checking the tbody elements textr value. So the entire string is "Booked, Booked, Booked, 9:00 AM, Booked, Booked, Booked, 2:00 Am, Booked". I think what you WANTED to do was loop through the individual cells? Like this:

#here you fetch all the ONE element that are selected by xpath:
rwdata = driver.find_element_by_xpath(
    "/html/body/div[2]/div/div/div/form[2]/div[2]/table/tbody")

#here you loop through all those table TD elements
for r in rwdata.find_elements_by_xpath("//td"):
    #here you print the TD's text value
    print(r.text)
    #here you check if the text value is equal to a short string
    if r.text != "Booked":
        print("Aceptado")

Upvotes: 1

Related Questions