【问题标题】:Click and view more pages with Selenium in Python单击并查看更多使用 Python 中的 Selenium 的页面
【发布时间】:2015-10-13 11:49:33
【问题描述】:

问题 我需要使用 Selenium 进入 page 中的所有顶级用户配置文件。 热门用户个人资料位于页面右侧。

我做了什么

    self.driver.get(response.url)
    user_list = self.driver.find_elements_by_xpath('//table[contains(@class,"W-100 Bc-c")]/tbody/tr')
    for single_user in user_list:
        single_user.find_element_by_xpath('.//td/a').click()
        time.sleep(3)

但我收到此错误消息:

WebDriverException:消息:未知错误:元素不可点击 在点 (865, 685)。其他元素会收到点击:

<div id="MouseoverMask" class="End-0 Start-0 T-0 B-0"></div>

信息 Python 2.7.10 硒 2.48 Pycharm

编辑+

我尝试打印名称并且它有效:

   print(str( single_user.find_element_by_xpath('.//td/a').text ) )

但是 click() 没有。

【问题讨论】:

  • 如果你运行它会发生什么?错误是什么?
  • 您的 xpath 看起来不错,那么问题出在哪里?
  • WebDriverException:消息:未知错误:元素在点 (865、685) 处不可点击。其他元素会收到点击:
  • 如果您查看 Selenium 所看到的 HTML,它完全是空白的(或者它对我来说是空白的) - 所以雅虎可能正在采取措施防止抓取。
  • @user3468054 mhhh 我使用 selenium 来获取其他内容,例如问题的 url,它可以工作。但是,如果您使用调试查看 user_list,并且如果您添加 print("example") 而不是 .click(),则可以为任何 single_user 执行 for

标签: python selenium xpath selenium-webdriver


【解决方案1】:

如果你确定你得到的对象是正确的,问题通常是:

  • 对象不可见
  • 当您尝试单击对象时页面未完全加载。

所以看看 Selenium 提供的 Wait 方法,确保你的对象是可见的

为了等待元素可点击:

from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID,'someid')))

在您的情况下,您可以尝试使用您获得的 id 找到每个元素并单击它:

self.driver.get(response.url)
user_list = self.driver.find_elements_by_xpath('//table[contains(@class,"W-100 Bc-c")]/tbody/tr')
for single_user in user_list:
    id = single_user.find_element_by_xpath('.//td/a').get_attribute("id")
    self.driver.find_elements_by_id(id).click()
    time.sleep(3) 

【讨论】:

  • 是的对象是可见的,在我等待大约 5 秒之前。
  • 您是否尝试等待我刚刚在帖子中添加的表格?
  • With wait = WebDriverWait(self.driver, 10) element = wait.until(EC.element_to_be_clickable((By.ID,'ya-leader-board')))
  • 元素在点 (871, 693) 处不可点击。其他元素会收到点击:
  • 你能测试我上次的编辑吗(我不确定获取 id 属性的语法)
【解决方案2】:

我没有看到任何错误,但在第一次点击后,网页元素已更改,因此您将无法获得之前在 xpath 中捕获的下一个网页元素。顺便试试下面的代码-

from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get('https://answers.yahoo.com/dir/index/discover?sid=396545663')
user_list = driver.find_elements_by_xpath('//table[contains(@class,"W-100 Bc-c")]/tbody/tr')
lnks = [i.find_element_by_xpath('.//td/a').get_attribute('href') for i in user_list]
for single_user in lnks:
    driver.get(single_user)
    time.sleep(3)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 2020-02-11
    相关资源
    最近更新 更多