使用 Selenium 从 Google 下拉菜单中进行选择时遇到问题 (Python)
Trouble selecting from Google Dropdown menu using Selenium (Python)
我正在尝试使用 Python 在 Google 的专利网站上自动搜索。这是 link 到网站 https://patents.google.com/advanced
我需要弄清楚如何从下拉菜单中选择 select。例如,从专利局下拉菜单中,我需要 select US 作为国家。
我尝试使用 Selenium find_element_ by_xpath 识别下拉菜单。按照本网站上建议的解决方案,我尝试使用 Selenium select_by visible_text 单击菜单。我没有成功。
我是 Python 和 html 的新手,所以任何线索都将不胜感激。
这是我试图从专利局下拉列表中找到的 Python 代码和 select 美国作为国家/地区。
#Identify the dropdown menu for Patent Office
off_element=driver.find_element_by_xpath("//dropdown-menu[@change-action='RESTRICT_OFFICE_MENU']/iron-dropdown/div[@id='contentWrapper']/div[@id='menu']/div[@class='style-scope dropdown-menu']")
#Declare the dropdown as an instance of the Select class.
patoff= Select(off_element)
patoff.selectByVisibleText("US")
这是我得到的错误
"UnexpectedTagNameException: Message: Select only works on select elements, not on div"
但是,我在 html 代码中没有看到任何 select 元素。我的 xpath 不正确吗?下拉列表不可见?下面我为下拉菜单粘贴了 html 代码。我不确定应该提供多少细节,所以我复制了整个路径。国家列表在最底部。
谢谢!
<dropdown-menu show-label="true" multi="" label="Patent Office" change-action="RESTRICT_OFFICE_MENU" class="style-scope restrict-editor">
<span class="label style-scope dropdown-menu">
<span class="style-scope dropdown-menu">Patent Office</span>
<span id="selected" class="style-scope dropdown-menu" hidden="">
<span style="margin: 0px 4px 0px 2px" class="style-scope
dropdown-menu">·</span>
<span class="style-scope dropdown-menu"></span>
</span>
<iron-icon icon="icons:arrow-drop-down" class="style-scope
dropdown-menu x-scope iron-icon-0"><svg viewBox="0 0 24 24"
preserveAspectRatio="xMidYMid meet" focusable="false"
class="style-scope iron-icon" style="pointer-events: none;
display: block; width: 100%; height: 100%;"><g class="style-
scope iron-icon"><path d="M7 10l5 5 5-5z" class="style-scope
iron-icon"></path></g></svg>
</iron-icon><iron-icon icon="icons:arrow-drop-up" class="style-scope
dropdown-menu x-scope iron-icon-0" hidden=""><svg viewBox="0 0 24
24" preserveAspectRatio="xMidYMid meet" focusable="false"
class="style-scope iron-icon" style="pointer-events: none; display:
block; width: 100%; height: 100%;"><g class="style-scope iron-
icon"><path d="M7 14l5-5 5 5z" class="style-scope iron-icon"> .
</path></g></svg>
</iron-icon>
</span>
<iron-dropdown allow-outside-scroll="" horizontal-align="left"
vertical-align="top" class="style-scope dropdown-menu" aria-
disabled="false" aria-hidden="true" style="outline: none; display:
none;"
<div id="contentWrapper" class="style-scope iron-dropdown">
<div class="dropdown-content style-scope dropdown-menu"
slot="dropdown-content" id="menu" multi="">
<div class="style-scope dropdown-menu">
<div class="item style-scope dropdown-menu">WO</div>
<div class="item style-scope dropdown-menu">US</div>
<div class="item style-scope dropdown-menu">EP</div>
<div class="item style-scope dropdown-menu">JP</div>
<div class="item style-scope dropdown-menu">KR</div>
<div class="item style-scope dropdown-menu">CN</div>
<div class="item style-scope dropdown-menu">AE</div>
以下代码适用于我,使用 Chrome 驱动程序:
import time
import urllib.request
import urllib.response
import re
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
import webbrowser
import requests, sys
import requests, csv
from lxml import html
import lxml.html
import requests
Chrome = webdriver.Chrome(executable_path='enter_your_driver_file_path_here')
url = "https://patents.google.com/advanced"
Chrome.get(url)
#Find Patent Office Menu.
patent_menu = Chrome.find_element_by_xpath("//*/span[@class='style-scope dropdown-menu' and text()='Patent Office']")
#Open the Patent Office menu.
patent_menu.click()
#Wait long enough for the patent menu to open, before selecting the country.
Chrome.implicitly_wait(10)
#Find the patent country option, which in this case is the 'US'.
patent_country = Chrome.find_element_by_xpath("//*/div[@id='menu']/div[@class='style-scope dropdown-menu']/div[@class='item style-scope dropdown-menu' and text()='US']")
#Select the patent country option, which in this case is the 'US'.
patent_country.click()
我正在尝试使用 Python 在 Google 的专利网站上自动搜索。这是 link 到网站 https://patents.google.com/advanced 我需要弄清楚如何从下拉菜单中选择 select。例如,从专利局下拉菜单中,我需要 select US 作为国家。
我尝试使用 Selenium find_element_ by_xpath 识别下拉菜单。按照本网站上建议的解决方案,我尝试使用 Selenium select_by visible_text 单击菜单。我没有成功。
我是 Python 和 html 的新手,所以任何线索都将不胜感激。 这是我试图从专利局下拉列表中找到的 Python 代码和 select 美国作为国家/地区。
#Identify the dropdown menu for Patent Office
off_element=driver.find_element_by_xpath("//dropdown-menu[@change-action='RESTRICT_OFFICE_MENU']/iron-dropdown/div[@id='contentWrapper']/div[@id='menu']/div[@class='style-scope dropdown-menu']")
#Declare the dropdown as an instance of the Select class.
patoff= Select(off_element)
patoff.selectByVisibleText("US")
这是我得到的错误
"UnexpectedTagNameException: Message: Select only works on select elements, not on div"
但是,我在 html 代码中没有看到任何 select 元素。我的 xpath 不正确吗?下拉列表不可见?下面我为下拉菜单粘贴了 html 代码。我不确定应该提供多少细节,所以我复制了整个路径。国家列表在最底部。 谢谢!
<dropdown-menu show-label="true" multi="" label="Patent Office" change-action="RESTRICT_OFFICE_MENU" class="style-scope restrict-editor">
<span class="label style-scope dropdown-menu">
<span class="style-scope dropdown-menu">Patent Office</span>
<span id="selected" class="style-scope dropdown-menu" hidden="">
<span style="margin: 0px 4px 0px 2px" class="style-scope
dropdown-menu">·</span>
<span class="style-scope dropdown-menu"></span>
</span>
<iron-icon icon="icons:arrow-drop-down" class="style-scope
dropdown-menu x-scope iron-icon-0"><svg viewBox="0 0 24 24"
preserveAspectRatio="xMidYMid meet" focusable="false"
class="style-scope iron-icon" style="pointer-events: none;
display: block; width: 100%; height: 100%;"><g class="style-
scope iron-icon"><path d="M7 10l5 5 5-5z" class="style-scope
iron-icon"></path></g></svg>
</iron-icon><iron-icon icon="icons:arrow-drop-up" class="style-scope
dropdown-menu x-scope iron-icon-0" hidden=""><svg viewBox="0 0 24
24" preserveAspectRatio="xMidYMid meet" focusable="false"
class="style-scope iron-icon" style="pointer-events: none; display:
block; width: 100%; height: 100%;"><g class="style-scope iron-
icon"><path d="M7 14l5-5 5 5z" class="style-scope iron-icon"> .
</path></g></svg>
</iron-icon>
</span>
<iron-dropdown allow-outside-scroll="" horizontal-align="left"
vertical-align="top" class="style-scope dropdown-menu" aria-
disabled="false" aria-hidden="true" style="outline: none; display:
none;"
<div id="contentWrapper" class="style-scope iron-dropdown">
<div class="dropdown-content style-scope dropdown-menu"
slot="dropdown-content" id="menu" multi="">
<div class="style-scope dropdown-menu">
<div class="item style-scope dropdown-menu">WO</div>
<div class="item style-scope dropdown-menu">US</div>
<div class="item style-scope dropdown-menu">EP</div>
<div class="item style-scope dropdown-menu">JP</div>
<div class="item style-scope dropdown-menu">KR</div>
<div class="item style-scope dropdown-menu">CN</div>
<div class="item style-scope dropdown-menu">AE</div>
以下代码适用于我,使用 Chrome 驱动程序:
import time
import urllib.request
import urllib.response
import re
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
import webbrowser
import requests, sys
import requests, csv
from lxml import html
import lxml.html
import requests
Chrome = webdriver.Chrome(executable_path='enter_your_driver_file_path_here')
url = "https://patents.google.com/advanced"
Chrome.get(url)
#Find Patent Office Menu.
patent_menu = Chrome.find_element_by_xpath("//*/span[@class='style-scope dropdown-menu' and text()='Patent Office']")
#Open the Patent Office menu.
patent_menu.click()
#Wait long enough for the patent menu to open, before selecting the country.
Chrome.implicitly_wait(10)
#Find the patent country option, which in this case is the 'US'.
patent_country = Chrome.find_element_by_xpath("//*/div[@id='menu']/div[@class='style-scope dropdown-menu']/div[@class='item style-scope dropdown-menu' and text()='US']")
#Select the patent country option, which in this case is the 'US'.
patent_country.click()