点击Selenium + Python

我在 Selenium 中点击时遇到问题,它没有点击按钮。这是我的代码:


from selenium import webdriver

import time

import click

from selenium.webdriver.support.select import Select

from selenium.webdriver.common.keys import Keys



driver = webdriver.Chrome()


driver.get("https://help.instagram.com/contact/723586364339719/")


submit_button = driver.find_element_by_xpath('//*[@id="u_0_8"]')

submit_button.click()

这是 HTML 代码:


<button value="1" class="_42ft _4jy0 _4jy4 _4jy1 selected _51sy" type="submit" id="u_0_8">Enviar</button>


慕慕森
浏览 125回答 3
3回答

慕神8447489

您可以使用显式等待按钮来完成此操作:from selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as ECWebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='u_0_8']"))).click()我希望它能帮助你!

慕斯709654

所需的元素是动态元素,因此要单击该元素,您需要引发WebDriverWait ,并且element_to_be_clickable()可以使用以下任一 定位器策略:使用CSS_SELECTOR:driver.get('https://help.instagram.com/contact/723586364339719/')WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()使用XPATH:driver.get('https://help.instagram.com/contact/723586364339719/')WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[@value='1' and normalize-space()='Send']"))).click()注意:您必须添加以下导入:from selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as EC浏览器快照:

哔哔one

而不是这个:submit_button = driver.find_element_by_xpath('//*[@id="u_0_8"]')submit_button.click()尝试这个:driver.find_element_by_id("id='u_0_8']").click()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5