你可以试试下面的xpath。
//td[@class='titulo_lateral']
或
//td[text()='RELATÓRIOS' and contains(@onclick,'abreMenu')]
但首先你必须确定它在 HTMLDOM 中是否是唯一的。
PS:如果我们在HTML DOM 中有唯一条目,请检查dev tools(谷歌浏览器)。
检查步骤:
Press F12 in Chrome -> 转到element 部分 -> 做一个CTRL + F -> 然后粘贴xpath 看看,如果你想要的element 用@ 得到高亮 987654330@匹配节点。
一旦我们有了唯一匹配的 xpath。您可以尝试通过以下方法点击它。
代码试用 1:
time.sleep(5)
driver.find_element_by_xpath("//td[text()='RELATÓRIOS' and contains(@onclick,'abreMenu')]").click()
代码试用 2:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[text()='RELATÓRIOS' and contains(@onclick,'abreMenu')]"))).click()
代码试用 3:
time.sleep(5)
button = driver.find_element_by_xpath("//td[text()='RELATÓRIOS' and contains(@onclick,'abreMenu')]")
driver.execute_script("arguments[0].click();", button)
代码试用 4:
time.sleep(5)
button = driver.find_element_by_xpath("//td[text()='RELATÓRIOS' and contains(@onclick,'abreMenu')]")
ActionChains(driver).move_to_element(button).click().perform()
进口:
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.webdriver.common.action_chains import ActionChains