简体   繁体   English

我如何 select 在 python selenium 中具有相同 ID 的多个按钮

[英]How can I select multiple button with same id in python selenium

I have two series of a button with same id and class name.我有两个系列的按钮具有相同的 id 和 class 名称。

<div id="bedroomNum-input" class="pageComponent" data-label="CONFIG_BEDROOM">
    <span class="labels_semiBold radioInput_bubbleLable__1g8PH">No. of Bedrooms <div class="screeningActions_iconWrapper__dB1NM"></div></span>
    <div class="tagWrapper_tags_wrapper__KIRJJ  multiple_input" id="bedroomNum">
        <div id="1" class="pageComponent radioInput_textOnly__1r7GL" data-label="VALUE" data-custominfo="{&quot;custom_object&quot;:{&quot;value&quot;:&quot;1&quot;}}">
            <span>1</span>
        </div>
        <div id="2" class="pageComponent radioInput_textOnly__1r7GL" data-label="VALUE" data-custominfo="{&quot;custom_object&quot;:{&quot;value&quot;:&quot;2&quot;}}">
            <span>2</span>
        </div>
        <div id="3" class="pageComponent radioInput_textOnly__1r7GL" data-label="VALUE" data-custominfo="{&quot;custom_object&quot;:{&quot;value&quot;:&quot;3&quot;}}">
            <span>3</span>
        </div>
        <div id="4" class="pageComponent radioInput_textOnly__1r7GL" data-label="VALUE" data-custominfo="{&quot;custom_object&quot;:{&quot;value&quot;:&quot;4&quot;}}">
            <span>4</span>
        </div>
        <div class="hyperlinks_medium AddOther_addOtherLink__3jZ8s" style="display: block;">
            <i class="iconS_PPFDesk_16 icon_bluePlusIcon AddOther_addMoreIcon__24FzC"></i>
            <span class="AddOther_toggleLabel__YwU_k">Add other</span>
        </div>
    </div>
</div>

And Another one.还有一个。

<div class="tagWrapper_tags_wrapper__KIRJJ  multiple_input" id="bathroomNum">
    <div id="1" class="pageComponent radioInput_textOnly__1r7GLdata-label="VALUE" data-custominfo="{&quot;custom_object&quot;:{&quot;value&quot;:&quot;1&quot;}}">
        <span>1</span>
    </div>
    <div id="2" class="pageComponent radioInput_textOnly__1r7GL" data-label="VALUE" data-custominfo="{&quot;custom_object&quot;:{&quot;value&  quot;:&quot;2&quot;}}">
        <span>2</span>
    </div>
    <div id="3" class="pageComponent radioInput_textOnly__1r7GL" data-label="VALUE" data-custominfo="{&quot;custom_object&quot;:{&quot;value&quot;:&quot;3&quot;}}">
        <span>3</span>
    </div>
    <div id="4" class="pageComponent radioInput_textOnly__1r7GL" data-label="VALUE" data-custominfo="{&quot;custom_object&quot;:{&quot;value&quot;:&quot;4&quot;}}">
        <span>4</span>
    </div>
    <div class="hyperlinks_medium AddOther_addOtherLink__3jZ8s" style="display: block;">
        <i class="iconS_PPFDesk_16 icon_bluePlusIcon AddOther_addMoreIcon__24FzC"></i>
        <span class="AddOther_toggleLabel__YwU_k">Add other</span>
    </div>
</div>

I want to select both 1 button in under both div using selenium python.我想使用 selenium python 在两个 div 下的 select 两个 1 按钮。 How can I do it?我该怎么做?

From yout HTML, I can give you the answer,it is correct if you explained it well.从你的HTML,我可以给你答案,如果你解释得好是正确的。

button1=browser.find_element(By.ID,"bedroomNum-input")
button2=browser.find_element(By.ID,"bathroomNum")

For the first one对于第一个

driver.find_element_by_css_selector("#bedroomNum > div[id='1']")

For the second one对于第二个

driver.find_element_by_css_selector("#bathroomNum > div[id='1']")

These have both been tested and work in a mock HTML page based on your provided HTML.根据您提供的 HTML,这些都已经过测试并在模拟 HTML 页面中工作。

NOTE: CSS doesn't like an id of #1 , that's why I had to use [id='1'] .注意: CSS 不喜欢#1的 id,这就是我必须使用[id='1']的原因。

Though both the buttons are having the same value ie 1 for the id attribute but they are located at different location within the Viewport and you have you select ie invoke click() on them individually.虽然这两个按钮的id属性值相同,即1 ,但它们位于视口内的不同位置,并且您拥有 select,即分别在它们上调用click()

The desired element is a Ember.js enabled element, so ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :所需的元素是启用Ember.js的元素,因此理想情况下,单击需要为element_to_be_clickable()诱导WebDriverWait的元素,您可以使用以下任一定位器策略

  • XPath to click the first element: XPath点击第一个元素:

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='bedroomNum']//div[@id='1']/span[text()='1']"))).click()
  • CSS_SELECTOR to click the first element: CSS_SELECTOR点击第一个元素:

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#bedroomNum div#1 > span"))).click()
  • XPath to click the second element: XPath点击第二个元素:

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='bathroomNum']//div[@id='1']/span[text()='1']"))).click()
  • CSS_SELECTOR to click the second element: CSS_SELECTOR点击第二个元素:

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#bathroomNum div#1 > span"))).click()
  • Note : You have to add the following imports:注意:您必须添加以下导入:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python selenium中的相同按钮ID - Same button id in python selenium 如何使用相同的 xpath 单击 Python Selenium 中的多个项目? - How can I click multiple items in Python Selenium with the same xpath? Select 一个带有 selenium 的按钮,具有相同的 XPATH 但没有标签或 ID - Select a button with selenium with same XPATH but no tag or id 如何从Angular随机ID,Selenium + Python中选择按钮类 - How to select a button classes from Angular random id, Selenium + Python Python Selenium 如何单击此按钮 - Python Selenium How can I click this button 如何使用 selenium python 在多个类中使用 select 按钮? - How to select button inside of multiple classes with selenium python? selenium python 如何找到以相同开头的所有 ID 元素 - selenium python How can I find all ID elements that begin the same 如何通过Selenium / Python选择没有ID的Web元素 - How do I select a web element with no ID through Selenium/Python 如何在 Python 和 Selenium 中找到具有相同 class 名称的多个用户名? - How can I find multiple usernames that have the same class name in Python with Selenium? 在 Playwright (Python) 中,当页面上有多个按钮时,它们都具有相同的名称,我如何 select 正确的按钮? - In Playwright (Python) when there are multiple buttons on a page, all with the same name, how do i select correct button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM