简体   繁体   中英

Python selenium can't click multiple div because of same class

I am trying to achieve multiple div to get a click in the same class but it's ain't so helpful [Sit Plan][1] https://ibb.co/SfdT2LW

This is the code

<a href="javascript:void(0)" class="seat" title="[GHA-15]" data-toggle="tooltip" data-placement="bottom" onclick="chooseSeat(this)">0-4-5                                                                
                                <div class="spinner">
                                                                    <div class="double-bounce1"></div>
                                                                    <div class="double-bounce2"></div>
                                                                </div>
                                                            </a>

<a href="javascript:void(0)" class="seat" title="[GHA-14]" data-toggle="tooltip" data-placement="bottom" onclick="chooseSeat(this)">0-2-5                                                                
                                <div class="spinner">
                                                                    <div class="double-bounce1"></div>
                                                                    <div class="double-bounce2"></div>
                                                                </div>
                                                            </a>

**That's how I tried . but work for single div only ** '''

div = driver.find_element_by_class_name("spinner")
div.click()'''

This is what i tried from Web. but is'nt helping
''' 
div1 = driver.find_elements_by_xpath('//a[@class="seat"]//preceding-sibling::td[@div="spinner"]')
# div1.click()

'''

Using following way you can select the required div when the multiple div have the same class name

select=browser.find_element_by_xpath('//*[@class="class name"][index of div]')

Here class name is the name of div class and the index is the index of div which you want to select it start from 1 to onward

to differentiate between

0-4-5 and 0-2-5 you can simply use the title attribute ( xpath ) from the shared HTML.

//a[@title='[GHA-15]']

should represent 0-4-5

//a[@title='[GHA-14]'] for 0-2-5

Click it like:

driver.find_element(By.XPATH, "//a[@title='[GHA-15]']").click()

or

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@title='[GHA-14]']"))).click()

Update:

There are multiple ways to click on the spinner element.

  1. Use XPath-indexing:

     driver.find_element(By.XPATH, "(//div[@class='spinner'])[1]").click()

    or

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(//div[@class='spinner'])[1]"))).click()
  2. Use find_elements

     elements = WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, "//div[@class='spinner']"))) elements[0].click()

    or

    elements[1].click()

    etc.

  3. click in a loop:

     elements = WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, "//div[@class='spinner']"))) for element in elements: element.click() time.sleep(3)

Imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

get all element with same class using:

elms = driver.find_elements(By.CLASS_NAME, "seat")

then iterate through the list to get what you are looking for

for elem in elems:
    if # your condition:
        # do your stuff

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM