我试着从下拉菜单中得到每个国家的价格。https://www.lycamobile.pl/po/internationalrates
但我发现几个问题:
1.按xpath查找元素不适用于下拉菜单:xpath id是动态的。我跳过了源代码中的国家列表。但其实我不应该这么做。
2.我需要在表格中出现的价格,点击国家后,发现元素方法在这种情况下也不起作用..即使使用CSS选择器。
我认为这是因为js在这个网站的后端,对吗?
发布于 2018-07-27 13:56:28
下面的代码可以帮助你搜索所有的国家,我不知道你在寻找什么价格,所以我没有尝试去获取价格。
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
https://stackoverflow.com/questions/-100005849
复制相似问题