0

我正在用硒编写一个用于火种的机器人,但它不起作用。这是我的代码。

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>
4

2 回答 2

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
于 2020-02-12T07:30:57.013 回答
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()

你可以在这里查看等待条件!

于 2020-02-12T07:31:13.347 回答