[英]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="{"custom_object":{"value":"1"}}">
<span>1</span>
</div>
<div id="2" class="pageComponent radioInput_textOnly__1r7GL" data-label="VALUE" data-custominfo="{"custom_object":{"value":"2"}}">
<span>2</span>
</div>
<div id="3" class="pageComponent radioInput_textOnly__1r7GL" data-label="VALUE" data-custominfo="{"custom_object":{"value":"3"}}">
<span>3</span>
</div>
<div id="4" class="pageComponent radioInput_textOnly__1r7GL" data-label="VALUE" data-custominfo="{"custom_object":{"value":"4"}}">
<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="{"custom_object":{"value":"1"}}">
<span>1</span>
</div>
<div id="2" class="pageComponent radioInput_textOnly__1r7GL" data-label="VALUE" data-custominfo="{"custom_object":{"value& quot;:"2"}}">
<span>2</span>
</div>
<div id="3" class="pageComponent radioInput_textOnly__1r7GL" data-label="VALUE" data-custominfo="{"custom_object":{"value":"3"}}">
<span>3</span>
</div>
<div id="4" class="pageComponent radioInput_textOnly__1r7GL" data-label="VALUE" data-custominfo="{"custom_object":{"value":"4"}}">
<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.