0

For example a site

I need to script clicking "close" button on an appeared frame.

I'm already tried using xpath, css_selection still worthless.

Need to do stuff using headless-browser, like HtmlUnit

Because there is no "a"-tag.

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()

But the last part isn't working (can't close frame).

How to do this ?

kAldown
  • 610
  • 2
  • 8
  • 27

2 Answers2

1

Try this. this should work.

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()
mchackam
  • 81
  • 4
  • selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//div[@class=\"frame _num_2 _pos_last _moverDir_left _active _show _state_visible _ready _cont_firmCard _mover\"]/div/div/div[@class=\"frame__controlsButton _close\"]"} – kAldown Jan 15 '16 at 19:36
  • this is wierd. in my machine it opens the page with a frame under search and clicks on the first link which inturn opens another frame close to it. and after that it closes the new frame. – mchackam Jan 15 '16 at 19:42
  • dunno, just full copy of this code. On debian, python 2.7 – kAldown Jan 15 '16 at 19:46
  • I'm linux mint. python 2.7. using pycharm IDE – mchackam Jan 15 '16 at 19:50
  • are you sure? I'm just created docker image witch doesn't work either – kAldown Jan 16 '16 at 11:11
  • here's the youtube link for the screencast.[youtube link](https://youtu.be/vIKYj31sgK4) – mchackam Jan 16 '16 at 11:38
  • Yes, friends told me that it works. Problem I guess on me, using tiled-dm, like `i3`, which incompatible with adaptive design on this site. Now last one - how to do this with headless browsers like `phantomjs`, or `htmlunit` – kAldown Jan 18 '16 at 11:39
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")]')

That is the answer for any driver.set_window_size() But need to find headless.

kAldown
  • 610
  • 2
  • 8
  • 27