选择xpath-硒

时间:2020-02-12 07:18:55

标签: python selenium

我正在使用selenium编写一个机器人用于火种,但它无法正常工作。这是我的代码。

fb_button =  self.driver.find_element_by_xpath('//*[@id="content"]/div/div[1]/div/div/main/div/div[2]/div[2]/div/div/span/div[2]/button')

fb_button.click()

此代码用于单击Facebook登录按钮。但是,当我运行代码时,它无法正常运行。我收到这样的错误。

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate e
lement: {"method":"xpath","selector":"//*[@id="content"]/div/div[1]/div/div/main/div/div[2]/div
[2]/div/div/span/div[2]/button"}

但是,当我尝试创建对象并尝试调用此函数时,它将返回某些内容。

按钮的HTML:

<button type="button" class="button Lts($ls-s) Z(0) CenterAlign Mx(a) Cur(p) Tt(u) Bdrs(100px) Px(24px) Px(20px)--s Py(0) Mih(42px)--s Mih(50px)--ml button--outline Bdw(2px) Bds(s) Trsdu($fast) Bdc(#fff) C(#fff) Bdc(#fff):h C(#fff):h Bdc(#fff):f C(#fff):f Bdc(#fff):a C(#fff):a Fw($semibold) focus-button-style W(100%) Fz(4vw)--ms" draggable="false" aria-label="Facebook ile oturum aç"><span class="Pos(r) Z(1) D(ib)">Facebook ile oturum aç</span></button>

2 个答案:

答案 0 :(得分:2)

您可以使用xpath单击元素:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Facebook ile oturum aç']"))).click()

注意:您必须添加以下导入:

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

答案 1 :(得分:2)

您可以等到元素可以单击并显示在HTML dom中。

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

fb_button = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "your_xpath")))
fb_button.click()

您可以检查等待条件here

相关问题