Python: click() function on xpath query result issue -


i'm having issue trying click on a href tag xpath query, line in question element = atag.xpath("./a"), error saying error: 'list' object has no attribute 'click'.

any appreciated.

import time import os.path import lxml.html lh import re import sys selenium import webdriver random import randint  params = sys.argv url = params[1] baseurl = url[:url.rfind('/')+1]  try:     page_number = 1      #--------------------------------------------------     ## initial page      driver = webdriver.firefox()     driver.get(params[1])      #--------------------------------------------------     ## page count      # give page time load     time.sleep(2)      page_raw = driver.page_source     page_raw = lh.fromstring(page_raw)     page_count_raw = page_raw.xpath("//div[contains(@class, 'menu')]/div/ul/li")     page_count = len(page_count_raw) - 2      #--------------------------------------------------     ## page if it's not page 1      while page_number <= page_count:         #--------------------------------------------------         # delay page processing random number of seconds 2-5         time.sleep(randint(2,5))         #--------------------------------------------------         ## create empty file         file_name = params[3] + 'json/' + time.strftime("%y%m%d%h") + '_' + str(params[2]) + '_' + str(page_number) + '.json'         #--------------------------------------------------         ## create json file if doesn't exist         if os.path.exists(file_name)==false:             json_file = open(file_name, "a+", encoding="utf-8")         else:             json_file = open(file_name, "w", encoding="utf-8")         json_file.write("{")         #--------------------------------------------------         # click page next page if not page 1         if page_number > 1:             index = 1             atag in page_count_raw:                 if index == (page_number + 1):                     element = atag.xpath("./a")                     element.click()                 index += 1         #--------------------------------------------------         ## proces page         #todo         #--------------------------------------------------         ## close webdriver         driver.quit()         #--------------------------------------------------         ## close json file         json_file.write("}")         json_file.close()         #--------------------------------------------------         ## increment page number         page_number += 1         #--------------------------------------------------   except exception e:     print('error: ' + str(e.args[0])) 

you mixed lxml code selenium code. element list returned lxml code, it's not webelement or list of webelements , can't apply click() if try element[0].click().

i'd suggest avoid using lxml seem redundant in case. try parse page source selenium built-in methods.

if need list of div elements can use:

page_count_raw = driver.find_elements_by_xpath("//div[contains(@class, 'menu')]/div/ul/li") 

to find child a element:

for div in page_count_raw:     element = div.find_element_by_xpath('./a') 

note if defined page_count_raw on first page, it not accessible on next page, can scrape list of links , each link in loop. like:

links = [link.get_attribute('href') link in driver.find_elements_by_xpath("//div[contains(@class, 'menu')]/div/ul/li/a")] link in links:     driver.get(link) 

if need more details update ticket specific description problem not quite clear


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -