无法单击具有唯一按钮的按钮 数据订单组 ID 的信息 python+Selenium



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>

在上面的javascript代码片段中,每个订单都有多个(每个订单的按钮数据订单组id不同(。我还想为每个订单点击REPEAT order按钮。如何在这段代码中做到这一点?

您可以尝试使用以下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

@cruisepandy您的代码运行良好,但由于网站的特定条件,即帮助中心固定框和订单重复按钮可能会以某种方式重叠。这就是为什么我使用";window.rollTo";之前除了你的代码

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)

最新更新