无法点击selenium

时间:2018-06-12 06:24:49

标签: python-2.7 selenium selenium-webdriver xpath selenium-chromedriver

我试图点击此网站上包含电话号码的元素,请在下方链接。它是" Toon Nummer"

element location

找到元素很容易:

tel = driver.find_element_by_xpath("//button[contains(@title, 'telefoon')]")


但是如果我想点击它,到目前为止我知道两种方式:

tel.click()

这只返回ElementNotVisibleException。另一种方式:

driver.execute_script("arguments[0].click();", tel)

这不做任何事情,没有错误但没有点击,因为信息没有显示。我还能做些什么来成功点击这个?
link to site

2 个答案:

答案 0 :(得分:1)

试试这个:

phone_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//div[@id='vip-seller']/following-sibling::section/child::button")))  
phone_button .click()  

确保输入以下内容:

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

您编写的Xpath包含两个Web元素。希望这会有所帮助。

答案 1 :(得分:0)

要点击文字为 Toon nummer 的元素,您必须引导 WebDriverWait 以使元素可点击,您可以使用以下解决方案:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 
# other lines of code
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//aside[@class='l-side-right']//button[@class='mp-Button mp-Button--secondary' and @title='Toon telefoonnummer']//span[contains(.,'Toon')]"))).click()