【问题标题】:unable to click button with unique button data-order-group-id 's information python+selenium无法单击具有唯一按钮 data-order-group-id 信息的按钮 python+selenium
【发布时间】:2021-09-15 18:08:40
【问题描述】:
javascript代码(重复顺序1):
<div class="col-ea-1 repeat-order repeatable-order"> #common row in other snippets
<button data-order-group-id="a9755447-04ff-4d00-59bf-06ff87f8ead6" #different row
data-restaurant-seo-url="/abc-pizza-bayburt-kirazli-mah"
class="ys-btn ys-btn-primary ys-btn-aa middle repeat-order-button"> REPEAT ORDER </button> ==$0
</div>
在上面的sn-p javascript代码中,每个订单有多个(每个订单的按钮data-order-group-id不同)我也想到达并单击每个订单的REPEAT ORDER按钮如何我可以在这段代码中做到这一点吗?
【问题讨论】:
标签:
javascript
python
python-3.x
selenium
selenium-webdriver
【解决方案1】:
你可以试试下面的xpath:
//button[contains(text(), 'REPEAT ORDER') and contains(@class, 'repeat-order-button')]
find_element 用于单行,find_elements 用于多行。
示例代码:
for ele in driver.find_elements(By.XPATH, "//button[contains(text(), 'REPEAT ORDER') and contains(@class, 'repeat-order-button')]"):
ele.click()
# do some other stuff
# re-initiate elements here otherwise you will get stale element reference
#break if requires
【讨论】:
-
当它传递给for循环中的第二个元素时会出现这样的错误: ElementClickInterceptedException ...class="ys-btn ys-btn-primary ys-btn-aa 中间重复顺序-button">... 在点 (1075, 598) 是不可点击的。其他元素会收到点击: ...
,其实帮助中心是固定在网站一角的,我没有不明白为什么会影响代码
【解决方案2】:
@cruisepandey 您的代码运行良好,但由于
网站的特定条件是帮助中心固定框和 ORDER REPEAT 按钮可以以某种方式重叠这就是为什么我之前除了你的代码之外还使用了“window.scrollTo”
i=int(input("choose:"))
if i==0:
driver.execute_script("window.scrollTo(0, 300)") #move the area
driver.find_elements(By.XPATH, "//button[contains(text(), 'REPEAT ORDER') and contains(@class, 'repeat-order-button')]")[0].click()
sleep(7)
elif i==1:
driver.execute_script("window.scrollTo(0, 300)") #move the area
driver.find_elements(By.XPATH, "//button[contains(text(), 'REPEAT ORDER') and contains(@class, 'repeat-order-button')]")[1].click()
sleep(7)
elif i==2:
driver.execute_script("window.scrollTo(0, 600)") #move the area
driver.find_elements(By.XPATH, "//button[contains(text(), 'REPEAT ORDER') and contains(@class, 'repeat-order-button')]")[2].click()
sleep(7)