繁体   English   中英

如何循环 python 动作直到按钮可点击(硒)

[英]How to loop a python action until a button is clickable (selenium)

我在编码方面非常缺乏经验,我在这里的所有工作都是通过研究完成的。

我正在创建一个 python 脚本,它可以帮助我通过 selenium 将票添加到购物篮,但是遇到一些事情我不知道该怎么做。

票务网站需要坐下来刷新页面,直到另一个用户可以提供票,然后一个按钮变为可点击,然后您可以预订它。

我已经创建了脚本的第一部分,它打开并链接并在可用时单击按钮,但是当它不可用时,我需要刷新页面并尝试单击可用的按钮并重复,直到希望成功为止脚本可以停止。

当一张票被添加到购物篮时,URL 会发生变化,因此这可能是脚本在停止之前检查的条件。

下面是 python 代码,其中包含 URL 链接,其中按钮不可点击。

要测试脚本工作,请将 URL 更改为: https://ticketing.liverpoolfc.com/en-GB/events/liverpool%20women%20v%20everton%20women/2022-9-25/an?hallmap

需要点击的按钮是 CHOOSE SEAT FOR ME

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

PATH = "D:\chromedriver.exe"

driver = webdriver.Chrome(PATH)

driver.get("https://ticketing.liverpoolfc.com/en-GB/events/liverpool%20v%20newcastle%20united/2022-8-31_20.00/anfield?hallmap")


try:
    element = WebDriverWait(driver, 25).until(
        EC.element_to_be_clickable((By.XPATH,"/html/body/div[7]/div/button"))
    )
finally:
    print("Page loaded")

button = driver.find_element(By.XPATH, "/html/body/div[7]/div/div[4]/div[1]/div[3]/div[2]/div[2]/div/div[3]/div[1]/button[2]")
button.click()```




正如我从您的代码中看到的那样,您实际上并没有单击/html/body/div[7]/div/button按钮。 您在这里实际要做的就是单击/html/body/div[7]/div/div[4]/div[1]/div[3]/div[2]/div[2]/div/div[3]/div[1]/button[2]按钮。
如果This event is sold out. Please try again later This event is sold out. Please try again later通知。
如果是这样,您可以使您的代码更加清晰和简单。
可以稍等片刻找到/html/body/div[7]/div/div[4]/div[1]/div[3]/div[2]/div[2]/div/div[3]/div[1]/button[2]按钮启用。 如果发现此元素已禁用 - 刷新页面。
如果发现该按钮已启用 - 单击它。
您还可以改进定位器。 如下:

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

PATH = "D:\chromedriver.exe"

driver = webdriver.Chrome(PATH)

driver.get("https://ticketing.liverpoolfc.com/en-GB/events/liverpool%20v%20newcastle%20united/2022-8-31_20.00/anfield?hallmap")

while true:
    try:
        #try to find the button enabled
        #in case you found it enabled - click it
        WebDriverWait(driver, 5).until(
        EC.element_to_be_clickable((By.XPATH,"//button[@class='areas-filter-panel__find-button' and(not(@disabled))]")).click()
        #break the loop in case of successful click
        break
    except:
        #if button found disabled exception is thrown, catch catches it and performs a refresh
        driver.refresh()

要刷新网站的元素选择座位让我可以点击,您需要为element_to_be_clickable()诱导WebDriverWait并将点击事件包装在while-try-except{}块中,您可以使用以下定位器策略

driver.get('https://ticketing.liverpoolfc.com/en-GB/events/liverpool%20women%20v%20everton%20women/2022-9-25_18.45/anfield?hallmap')
while True:
    try:
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.areas-filter-panel__find-button"))).click()
        break
    except TimeoutException:
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='ui-button showPopupMessage-redbutton ui-corner-all ui-widget' and text()='OK']"))).click()
        driver.refresh()
        continue
# other lines of code
driver.quit()

注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
 from selenium import webdriver
 from selenium.webdriver.common import action_chains
 from selenium.webdriver.common.keys import Keys
 from selenium.webdriver.common.by import By
 from selenium.webdriver.support.ui import WebDriverWait
 from selenium.webdriver.support import expected_conditions as EC
 import time
 from selenium.webdriver.common.action_chains import ActionChains
  
 driver = webdriver.Chrome()

While True:
 try:

 time.sleep(5)

 ActionChains(driver).move_to_element(
 driver.find_element(By.XPATH,'button').perform()

 if driver.find_element(By.XPATH,'button').is_enabled():
    driver.find_element(By.XPATH,'button').click()
    print('clicked')
    break
 else:
   driver.refresh()
 except Exception as e:
 print(f'button not found: {e}')
 continue  

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM