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 ?

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

7 Comments

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\"]"}
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.
dunno, just full copy of this code. On debian, python 2.7
I'm linux mint. python 2.7. using pycharm IDE
are you sure? I'm just created docker image witch doesn't work either
|
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.