python selenium can可以单击多个Div,因为同一类

发布于 2025-01-21 16:11:26 字数 1728 浏览 0 评论 0原文

我正在尝试获得多个Div以在同一班级中获得单击,但这并不是那么有用 [SIT计划] [1] https://ibb.co/sfdt2lw

代码

<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>

这是我尝试的 **。但是只为单个Div工作** '''

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()

'''

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()

'''

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那伤。 2025-01-28 16:11:26

要区分

0-4-50-2-5您可以简单地使用标题属性xpath)来自共享HTML。

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

应表示0-4-5

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

单击就像:

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

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

更新:

有多种单击旋转器元素的方法。

  1. 使用xpath-indexing:

      driver.find_element(by.xpath,”(// div [@class ='spinner'])[1])。
     

      webdriverwait(驱动程序,20).until(ec.element_to_be_be_clickable(((by.xpath,“(// div [@class ='spinner']))[1]))。
     
  2. 使用find_elements

      elements = webdriverwait(驱动程序,20).until(ec.presence_of_all_ellements_located(((by.xpath,“ // div class ='spinner'))
    元素[0] .click()
     

     元素[1] .click()
     

    等。

  3. 单击循环:

      elements = webdriverwait(驱动程序,20).until(ec.presence_of_all_ellements_located(((by.xpath,“ // div class ='spinner'))
    对于元素中的元素:
        element.Click()
        时间。
     

进口:

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

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
欲拥i 2025-01-28 16:11:26

使用以下方式,您可以选择所需的div当多个DIV具有相同的类名称

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

此类名称是Div类的名称,索引是Div的索引,您想选择它从1到开始

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

孤星 2025-01-28 16:11:26

使用同一类获取所有元素:

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

然后在列表中迭代以获取您要寻找的东西

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文