使用硒单击多个链接

时间:2019-12-05 13:19:15

标签: python selenium

试图将更大的问题分解成小块 main question

我当前正在将拳击手的姓名输入自动填充框中,选择出现的第一个选项(拳击手的姓名),然后单击查看更多,直到获得所有拳击手打架的列表,并且不再显示查看更多按钮。

然后,我尝试创建一个onclick hrefs列表,我想单击该列表,然后迭代单击每个列表,并从每个页面/页面中获取html。理想情况下,我特别希望提取文本。

这是我编写的代码:

page_link = 'http://beta.compuboxdata.com/fighter'
chromedriver = 'C:\\Users\\User\\Downloads\\chromedriver'
cdriver = webdriver.Chrome(chromedriver)
cdriver.maximize_window()
cdriver.get(page_link)
wait = WebDriverWait(cdriver,20)
wait.until(EC.visibility_of_element_located((By.ID,'s2id_autogen1'))).send_keys('Deontay Wilder')
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'select2-result-label'))).click()
while True:
    try:
        element = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'view_more'))).click()
    except TimeoutException:
        break
# fighters =  cdriver.find_elements_by_xpath("//div[@class='row row-bottom-margin-5']/div[2]")
links = [x.get_attribute('onclick') for x in wait.until(EC.visibility_of_element_located((By.XPATH, "//*[contains(@onclick, 'get_fight_report')]/a")))]
htmls = []
for link in links:
    cdriver.get(link)
    htmls.append(cddriver.page_source)

但是运行此命令会给我错误消息:

ElementClickInterceptedException          Traceback (most recent call last)
<ipython-input-229-1ee2547c0362> in <module>
     10 while True:
     11     try:
---> 12         element = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'view_more'))).click()
     13     except TimeoutException:
     14         break
ElementClickInterceptedException: Message: element click intercepted: Element <a class="view_more" href="javascript:void(0);" onclick="_search('0')"></a> is not clickable at point (274, 774). Other element would receive the click: <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">...</div>
  (Session info: chrome=78.0.3904.108)

更新

我尝试查看一些带有类似错误消息的答案,并尝试了

while True:
    try:
        element = cdriver.find_element_by_class_name('view_more')
        webdriver.ActionChains(cdriver).move_to_element(element).click(element).perform()
#         element = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'view_more'))).click()
    except TimeoutException:
        break
links = [x.get_attribute('onclick') for x in wait.until(EC.visibility_of_element_located((By.XPATH, "//*[contains(@onclick, 'get_fight_report')]/a")))]
htmls = []
for link in links:
    cdriver.get(link)
    htmls.append(cddriver.page_source)

,但这似乎在ActionChains点处创建了某种无限循环。似乎一直在等待更多href出现

1 个答案:

答案 0 :(得分:0)

click函数应该已经移动了窗口,因此元素位于可见窗口中。因此,您不需要该动作链(我认为...),但是原始错误在“查看更多”按钮上显示了其他元素。

您可能需要从DOM中删除(或隐藏)此元素,或者如果它是html窗口,则“关闭”它。因此,精确定位此覆盖元素是关键,然后决定一种策略来发现“查看更多”按钮。

您的站点http://beta.compuboxdata.com/fighter当时似乎无法正常工作,因此我无法进一步进行挖掘。