如何从Selenium Python中的按钮中获取数据

时间:2019-11-23 06:47:35

标签: python selenium selenium-webdriver web-scraping selenium-chromedriver

我想从此按钮中删除数据我该如何使用Selenium Python

这是按钮的屏幕截图:

enter image description here

这是网站链接:https://www.zameen.com/EstateAgents/Islamabad-3-152.html

这是我的代码:

driver = webdriver.Chrome()
for i  in range(1,154):
    driver.get('https://www.zameen.com/EstateAgents/Islamabad-3-' + str(i) + '.html')
    name = driver.find_elements_by_css_selector('#agent_search_listing_section .title')
    number = driver.find_elements_by_css_selector('.totalofnum:nth-child(3)')
    Total_Number =driver.find_elements_by_css_selector('.titanium-con+ .clearfix')
    button = driver.find_elements_by_css_selector('#agent_phone').click()
    time.sleep(3)
    phone = driver.find_elements_by_css_selector('.phone_click')
    items = len(name)
    with open(csv,'a') as s:
        for item in range(items):
            s.write(name[item].text + ',' + number[item].text + ',' + Total_Number[item].text + ',' + phone[item].text + '\n')

我想从通话按钮中删除数据,但尝试时会收到此错误消息?

错误:

Traceback (most recent call last):
  File "scrap.py", line 16, in <module>
    button = driver.find_elements_by_css_selector('#agent_phone').click()
AttributeError: 'list' object has no attribute 'click'

1 个答案:

答案 0 :(得分:3)

问题是您正在使用find_elements_by_css_selector,请注意其中的elements,这就是为什么它返回列表且错误提示'list' object has no attribute 'click'的原因。因此,要么使用

button = driver.find_element_by_css_selector('#agent_phone').click()

button = driver.find_element_by_id('agent_phone').click()

请注意,我们此处使用的是element,而不是elements