Sean Choi
Sean Choi

Reputation: 73

How to extract the img title next to span as per the given HTML through Selenium and Python

I am making a web crawling for checking a kind of availability. I want to check the title of the specific time. However, if the title is 'NO', there is no href, otherwise there is a href. Therefore, it's xpath depends on the title. The title name changes every time. So i can't check by xpath.

If I want to check the availability of 09:00~11:00, how can do that?

I tried to find by XPATH. However, since the XPATH changes as I told, I can't to check the specific time i want.

Thanks in advance.

Below is the HTML code.

<span class="rs">07:00~09:00</span><img src="../images/reservation_btn04.gif" title="NO"><br>
<span class="rs">09:00~11:00</span><img src="../images/reservation_btn04.gif" title="NO"><br>
<span class="rs">11:00~13:00</span><img src="../images/reservation_btn04.gif" title="NO"><br>
<span class="rs">13:00~15:00</span><a href="./reg_add.asp?regdate=2018-09-16&amp;ftime=4&amp;ftype=1&amp;fac=C"><img src="../images/reservation_btn03.gif" title="YES"></a><br>
<span class="rs">15:00~17:00</span><a href="./reg_add.asp?regdate=2018-09-16&amp;ftime=5&amp;ftype=1&amp;fac=C"><img src="../images/reservation_btn03.gif" title="YES"></a><br>
<span class="rs">17:00~19:00</span><a href="./reg_add.asp?regdate=2018-09-16&amp;ftime=6&amp;ftype=1&amp;fac=C"><img src="../images/reservation_btn03.gif" title="YES"></a><br>
<span class="rs">19:00~21:00</span><img src="../images/reservation_btn04.gif" title="NO"><br>

Upvotes: 1

Views: 765

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193068

As per the HTML you have shared to check the availability of any timespan e.g. 09:00~11:00 you can use the following solution:

  • You can create a function() which will take an argument as the timespan and extract the availability as follows:

    def check_availability(myTimeSpan):
        print(driver.find_element_by_xpath("//span[@class='rs'][.='" + myTimeSpan + "']//following::img[1]").get_attribute("title"))
    
  • Now you can call the function check_availability() with any timespan as follows:

    check_availability("09:00~11:00")
    

Upvotes: 1

Shivam Mishra
Shivam Mishra

Reputation: 1439

If the text 09:00~11:00 is fixed, you can locate the img element like this -

element = driver.find_element_by_xpath("//span[@class='rs' and contains(text(),'09:00~11:00')]/following-sibling::img")

To check whether the title attribute of the element is "YES" -

if element.get_attribute("title") == 'YES':
    // do whatever you want

To get the href attribute of your required element-

source = driver.find_element_by_xpath("//span[@class='rs' and contains(text(),'09:00~11:00')]/following-sibling::img[@title='YES']/preceding-sibling::a").get_attribute("href")

Upvotes: 0

Related Questions