【发布时间】:2020-07-22 17:46:57
【问题描述】:
我想刮迭代散点图。 在此之前,我想更改国家/地区,但我没有选择我想要的国家/地区。
网站链接: https://vizhub.healthdata.org/tobacco/
错误是
---------------------------------------------------------------------------
UnexpectedTagNameException Traceback (most recent call last)
<ipython-input-46-fa99f3a62600> in <module>
1 cc=driver.find_element_by_xpath('//*[@id="location_id"]')
----> 2 select_English=Select(cc)
3 #select_English.select_by_visible_text('English (GB)')
/Applications/anaconda3/lib/python3.7/site-packages/selenium/webdriver/support/select.py in __init__(self, webelement)
37 raise UnexpectedTagNameException(
38 "Select only works on <select> elements, not on <%s>" %
---> 39 webelement.tag_name)
40 self._el = webelement
41 multi = self._el.get_attribute("multiple")
UnexpectedTagNameException: Message: Select only works on <select> elements, not on <div>
我从网站上复制了 xpath,显示它是一个选择元素。
这是我的代码
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from selenium.common.exceptions import NoSuchElementException
chrome_optionsme = Options()
chrome_optionsme.add_argument("--incognito")
chrome_optionsme.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(options=chrome_optionsme,
executable_path="path/chromedriver")
url="https://vizhub.healthdata.org/tobacco/"
driver.get(url)
driver.find_element_by_xpath('//*[@id="data"]').click()
cc=driver.find_element_by_xpath('//*[@id="location_id"]')
select_English=Select(cc)
其次,如何使用 selenium 提取迭代图的数据?
【问题讨论】:
-
还有
<div class="control" id="location_id">,位于您的选择元素之前。您可以随时使用//select[@id='location_id'] -
@JustinEzequiel 使用您提供的代码后,我尝试选择中国,但失败了。
select_English.select_by_visible_text('China')出现错误:NoSuchElementException: Message: Could not locate element with visible text: China -
很可能是因为该元素不可见。其实select元素本身就有
style="display: none"。这表明您通常需要通过单击某个其他元素(例如某处的选项卡或按钮)来使选择元素可见。 -
@JustinEzequiel 添加代码
cc.click()后显示错误ElementNotInteractableException: Message: element not interactable (Session info: chrome=84.0.4147.89)当我手动单击它时,它是空白的。 -
是的,因为选择元素是隐藏的(显示:无)。您需要与
//div[@id='s2id_location_id']中的元素进行交互
标签: python selenium google-chrome selenium-webdriver selenium-chromedriver