如何使用Selenium + Python单击链接

时间:2018-08-30 04:47:23

标签: python python-3.x selenium

我正在尝试使用Selenium + Python来一个接一个地打开多个链接,但无法一个一个地单击每个链接并从每个链接获取数据。

我尝试过:

for i in range(0, 1):

    list_links = driver.find_elements_by_tag_name('a')

    i = []     

    for i in list_links:

            link = i.get_attribute('href')
            my_list = [link]
            matching = [s for s in my_list if "https://www.magicbricks.com/propertyDetails/" in s]
            str_list = list(filter(None, matching))
            print (str_list)

我在打印上面的代码后得到了这个:

['https://www.magicbricks.com/propertyDetails/3-BHK-1865-Sq-ft-Multistorey-Apartment-FOR-Sale-Mulund-West-in-Mumbai&id=4d423335383538333735']
[]
['https://www.magicbricks.com/propertyDetails/1-BHK-520-Sq-ft-Multistorey-Apartment-FOR-Sale-Kandivali-West-in-Mumbai&id=4d423333323636353737']
[]
['https://www.magicbricks.com/propertyDetails/2-BHK-938-Sq-ft-Multistorey-Apartment-FOR-Sale-Sion-in-Mumbai&id=4d423332393937303531']
[]
['https://www.magicbricks.com/propertyDetails/2-BHK-672-Sq-ft-Multistorey-Apartment-FOR-Sale-Goregaon-West-in-Mumbai&id=4d423334393532373231']
[]
['https://www.magicbricks.com/propertyDetails/3-BHK-1325-Sq-ft-Multistorey-Apartment-FOR-Sale-Madh-in-Mumbai&id=4d423236343136393731']
[]

对于单击链接,尝试了此操作但不起作用:

for link in matching:

       link.click()
       print (link)

编辑:

我尝试过:

for s in my_list:

    if "https://www.magicbricks.com/propertyDetails/" in s:

        print (s)
        driver.find_element[s].click()

获取错误:driver.find_element[s].click() TypeError: 'method' object is not subscriptable

or 
driver.find_elements_by_link_text(s).click()

获取错误:AttributeError: 'list' object has no attribute 'click'

请提出一些建议。提前致谢。

1 个答案:

答案 0 :(得分:0)

因为您的列表包含字符串,但不包含Selenium的Web元素(只有在此之后,您才能执行“点击”操作)。

为此,您还需要从list_links列表中保存这些对象(根据“匹配”链接的更新版本)。

代码:

my_needed_links = []

list_links = driver.find_elements_by_tag_name("a")

# Get unique links.
for link in list_links:

    if "https://www.magicbricks.com/propertyDetails/" in link.get_attribute("href"):

        if link not in my_needed_links:

            my_needed_links.append(link)

# Go through of them and click on each.
for unique_link in my_needed_links:

    unique_link.click()
    # Switch to the tab.
    driver.switch_to_window(driver.window_handles[1])
    # Do some stuff inside tab.
    # Close the tab.
    driver.close()
    # Switch back to the main tab/window.
    driver.switch_to_window(driver.window_handles[0])

PS:不使用Web元素的另一种方法:在Web驱动程序中使用JavaScript并在选项卡/窗口中打开链接(您的matching列表)。

希望对您有帮助!