Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Selenium和Python单击href按钮_Python_Selenium_Selenium Webdriver - Fatal编程技术网

使用Selenium和Python单击href按钮

使用Selenium和Python单击href按钮,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我正在使用selenium和python自动化web任务。我尝试使用多个不同的功能来单击我需要的按钮: <a href="/crm/tab/Reports">Reports</a> .find_element_by_link_text("Reports").click() .find_element_by_id .find_element_by_name .find_element_by_class_name .find_element_by_css_selector

我正在使用selenium和python自动化web任务。我尝试使用多个不同的功能来单击我需要的按钮:

<a href="/crm/tab/Reports">Reports</a>

.find_element_by_link_text("Reports").click()
.find_element_by_id
.find_element_by_name
.find_element_by_class_name
.find_element_by_css_selector

。通过链接文本(“报告”)查找元素。单击()
.按\u id查找\u元素\u
.按\u名称查找\u元素\u
.按\u类\u名称查找\u元素\u
.通过\u css\u选择器查找\u元素\u

似乎无法使这项工作,任何建议将不胜感激

使用“通过xpath查找元素”。右键单击并从浏览器中复制XPATH。

使用显式等待可能会解决此问题。对于显式等待,可以使用ID或XPATH。我更喜欢使用XPATH。要获取XPATH,右键单击元素,单击Inspect,右键单击
,选择Copy,然后复制XPATH。现在我们有了XPATH,请执行以下操作:

button_xpath = "the xpath of your element"
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, button_xpath))).click()
上述代码将等待10秒钟,直到找到按钮元素。如果找不到元素,则会给出TimeoutException

下列进口是必要的:

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

谢谢,我已经尝试复制xpath,但它似乎仍然不起作用。请尝试等待。直到或睡眠,然后单击。