kAldown
kAldown

Reputation: 626

How to script behavior like clicking on SVG using python

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 ?

Upvotes: 0

Views: 438

Answers (2)

kAldown
kAldown

Reputation: 626

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.

Upvotes: 0

mchackam
mchackam

Reputation: 81

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

Upvotes: 1

Related Questions