例如site
我需要在出现的框架上单击“关闭”按钮。
我已经尝试过使用xpath,css_selection仍然没用。
需要使用无头浏览器,比如HtmlUnit
因为没有“a” - 标签。
from selenium import webdriver
from lxml import etree, html
url = "http://2gis.ru/moscow/search/%D1%81%D0%BF%D0%BE%D1%80%D1%82%D0%B8%D0%B2%D0%BD%D1%8B%D0%B5%20%D1%81%D0%B5%D0%BA%D1%86%D0%B8%D0%B8/center/37.437286%2C55.753395/tab/firms/zoom/11"
driver = webdriver.Firefox()
#driver = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.HTMLUNIT)
driver.get(url)
content = (driver.page_source).encode('utf-8')
doc = html.fromstring(content)
elems = doc.xpath('//article[@data-module="miniCard"]')
elem = elems[0]
# get element id to click on
el1_id = elem.attrib['id']
#simulate click to open frame
el1_to_click = driver.find_element_by_xpath('//article[@id="{0}"]//\
a[contains(@class, "miniCard__headerTitle")]'.format(el1_id))
el1_to_click.click()
#some stuff
pass
#now need to close this
close = driver.find_element_by_xpath('//html/body/div[1]/div/div[1]/div[2]/div[2]/div[2]/div/div[2]/div/div[2]/div[3]/div[1]/div[2]/svg/use')
close.click()
但最后一部分不起作用(不能关闭帧)。
怎么做?
答案 0 :(得分:1)
试试这个。这应该有效。
from selenium import webdriver
from lxml import etree, html
url = "http://2gis.ru/moscow/search/%D1%81%D0%BF%D0%BE%D1%80%D1%82%D0%B8%D0%B2%D0%BD%D1%8B%D0%B5%20%D1%81%D0%B5%D0%BA%D1%86%D0%B8%D0%B8/center/37.437286%2C55.753395/tab/firms/zoom/11"
driver = webdriver.Firefox()
driver.implicitly_wait(10)
# driver = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.HTMLUNIT)
driver.get(url)
content = (driver.page_source).encode('utf-8')
doc = html.fromstring(content)
elems = doc.xpath('//article[@data-module="miniCard"]')
elem = elems[0]
# get element id to click on
el1_id = elem.attrib['id']
# simulate click to open frame
el1_to_click = driver.find_element_by_xpath('//article[@id="{0}"]//\
a[contains(@class, "miniCard__headerTitle")]'.format(el1_id))
el1_to_click.click()
# some stuff
pass
# now need to close this
close = driver.find_element_by_xpath(
'//div[@class="frame _num_2 _pos_last _moverDir_left _active _show _state_visible _ready _cont_firmCard _mover"]/div/div/div[@class="frame__controlsButton _close"]')
close.click()
答案 1 :(得分:0)
close = driver.find_element_by_xpath('//div[@data-module="frame"]/\
div[@class="frame__content"]/div[@class="frame__controls"]/\
div[contains(@class, "frame__controlsButton")\
and contains(@class,"_close")]')
这是任何driver.set_window_size()
的答案
但需要找到无头。