为了账号安全,请及时绑定邮箱和手机立即绑定

选择 xpath - Selenium

选择 xpath - Selenium

狐的传说 2022-07-26 20:51:18
我正在用硒编写一个用于火种的机器人,但它不起作用。这是我的代码。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 element: {"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 回答

?
慕尼黑的夜晚无繁华

TA贡献1864条经验 获得超6个赞

你可以等到元素可以点击并出现在 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()


查看完整回答
反对 回复 2022-07-26
?
胡说叔叔

TA贡献1804条经验 获得超8个赞

您可以使用 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


查看完整回答
反对 回复 2022-07-26
  • 2 回答
  • 0 关注
  • 95 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号