逐个单击多个xpath

2024-04-26 05:04:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在循环中调用comment_按钮,而不必在它前面添加号码。我使用了通过xpath查找元素来选择页面上具有相同xpath的所有元素,但我的问题是我必须逐个手动调用它

loop = True
while loop:
    try:
        sleep(1)
        comment_buttons = driver.find_elements_by_xpath('//div[@data-testid="reply"]/div/div[1]')
        comment_buttons[0].click()
        sleep(4)
        driver.find_element_by_xpath(reply_paste).send_keys(word)
        sleep(1)
        element = driver.find_element_by_xpath("//input[@type='file']")                           
        driver.execute_script("arguments[0].style.display = 'block';", element)             
        #post image
        element.send_keys(random.choice(images))
        sleep(2)
        driver.find_element_by_xpath(reply_button).click()
        sleep(6)  
        comment_buttons[1].click()