首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python和Selenium:如何从动态表中获取数据?

Python和Selenium:如何从动态表中获取数据?
EN

Stack Overflow用户
提问于 2018-07-27 04:15:30
回答 1查看 0关注 0票数 0

我试着从下拉菜单中得到每个国家的价格。https://www.lycamobile.pl/po/internationalrates

但我发现几个问题:

1.按xpath查找元素不适用于下拉菜单:xpath id是动态的。我跳过了源代码中的国家列表。但其实我不应该这么做。

2.我需要在表格中出现的价格,点击国家后,发现元素方法在这种情况下也不起作用..即使使用CSS选择器。

我认为这是因为js在这个网站的后端,对吗?

EN

Stack Overflow用户

发布于 2018-07-27 13:56:28

下面的代码可以帮助你搜索所有的国家,我不知道你在寻找什么价格,所以我没有尝试去获取价格。

代码语言:txt
复制
import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys

driver = Chrome(executable_path=<path>)
driver.get('https://www.lycamobile.pl/po/internationalrates')

# expose the lnkCountrySearch 
time.sleep(5)
country_dd = driver.find_element_by_class_name('country-dd-open')
country_dd.click()

# get all the countries from DOM
time.sleep(5)
_countries = driver.find_elements_by_css_selector('a[id*="lnkCountrySearch"]')
countries = [x.get_attribute('title') for x in _countries]

# search country object
search_country = driver.find_element_by_id('txtSearchCountry')

# loop through all the countries and search
for country in countries:
    search_country.send_keys(country)
    search_country.send_keys(Keys.ENTER)
    time.sleep(5)

    # get the rates
    # clear the country before searching
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档