如何使用硒点击网页中的图像
How to click an image in a web page with selenium
我正在尝试单击页面上的按钮 (https://www1.nseindia.com/products/content/equities/equities/eq_security.htm)。
背景:我不是普通的 selenium 用户。只是想从网站上获取一些数据。从一些帮助页面了解了 selenium。
我无法点击“获取数据”按钮。
这是我在 工作正常 的其他事情上的进展。
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"]'))
朋友们有什么建议吗?
您可以使用requests and beautifulsoup获取数据。您可以使用 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)
我正在尝试单击页面上的按钮 (https://www1.nseindia.com/products/content/equities/equities/eq_security.htm)。
背景:我不是普通的 selenium 用户。只是想从网站上获取一些数据。从一些帮助页面了解了 selenium。
我无法点击“获取数据”按钮。
这是我在 工作正常 的其他事情上的进展。
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"]'))
朋友们有什么建议吗?
您可以使用requests and beautifulsoup获取数据。您可以使用 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)