猿问

选择 xpath - 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>


狐的传说
浏览 96回答 2
2回答

慕尼黑的夜晚无繁华

你可以等到元素可以点击并出现在 HTML dom 中。from selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.support.ui import WebDriverWaitfb_button = WebDriverWait(driver, 10).until(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EC.element_to_be_clickable((By.XPATH, "your_xpath")))fb_button.click()

胡说叔叔

您可以使用 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 WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as EC
随时随地看视频慕课网APP

相关分类

Python
我要回答