如何使用硒单击网页中的图像

时间:2020-01-16 08:35:22

标签: python image selenium click

我正在尝试单击页面(https://www1.nseindia.com/products/content/equities/equities/eq_security.htm)上的按钮。

背景:我不是普通的硒用户。只是尝试从网站获取一些数据。通过一些帮助页面了解硒。

某些无法点击“ 获取数据”按钮的方法。

这是我在其他一切正常工作中取得的进步。

url="https://www1.nseindia.com/products/content/equities/equities/eq_security.htm"

options = Options()
options.headless = False

browser = webdriver.Chrome(options=options, executable_path=r'/usr/bin/chromedriver')
browser.get(url)

select=Select(browser.find_element_by_id('dataType'))
select.select_by_value("priceVolumeDeliverable")

select=Select(browser.find_element_by_id('series'))
select.select_by_value("EQ")
#time.sleep(5)

select=browser.find_element_by_id('symbol')
select.clear()
select.send_keys("RELIANCE")

select=Select(browser.find_element_by_id('dateRange'))
select.select_by_value("3month")

我已经尝试过以下操作以单击“ 获取数据”。

submit_button = browser.find_element_by_xpath('//*[@id="submitMe"]')
submit_button.click()

browser.find_element_by_css_selector('.getdata-button').click()
browser.find_element_by_xpath("//button[@id='submitMe']").click();
browser.find_element_by_xpath('//a[img/@src="/common/images/btn-get-data.gif"]').click()
browser.find_element_by_id("get").click()
browser.find_element_by_id("submitMe").click()
browser.find_element_by_xpath("//div[@class='getdata-button']//div[@id='get']").click()

browser.find_element_by_css_selector('//*[@id="get"]').click()

browser.find_element_by_id("#get")
browser.execute_script("arguments[0].click();",browser.find_element_by_xpath('//input[@type="button" and @value="Get Results"]'))

有没有建议的朋友?

1 个答案:

答案 0 :(得分:0)

您可以使用获取数据。您可以使用params来获取所需的数据:

from bs4 import BeautifulSoup
import requests

headers = {
    'Accept': '*/*',
    'X-Requested-With': 'XMLHttpRequest',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36',
    'Referer': 'https://www1.nseindia.com/products/content/equities/equities/eq_security.htm',
}

params = (
    ('symbol', 'RELIANCE'),
    ('segmentLink', '3'),
    ('symbolCount', '2'),
    ('series', 'EQ'),
    ('dateRange', '3month'),
    ('fromDate', ''),
    ('toDate', ''),
    ('dataType', 'PRICEVOLUMEDELIVERABLE'),
)

response = requests.get('https://www1.nseindia.com/products/dynaContent/common/productsSymbolMapping.jsp',
                        headers=headers, params=params)
soup = BeautifulSoup(response.text, 'html.parser')
rows = soup.select("tr")

for th in soup.select("th"):
    print(th.text)

for i in range(1, len(rows)):
    table_data = rows[i].select("td")
    for td in table_data:
        print(td.text)