Polar bear
Polar bear

Reputation: 1

python selenium click <a> button

I'm making macro with selenium.

I want to click this button on this page

enter image description here

So, I used following code. but, It does not work.

driver.find_element_by_xpath('/html/body/div[1]/div[5]/div[1]/div[3]/div/div/section/div/a').click()

What code should i use?

Upvotes: 0

Views: 59

Answers (2)

furas
furas

Reputation: 143097

This button is inside <iframe id="down"> but Selenium treats frame as separated page and you have to first switch_to.frame before you can search inside frame.

frame = driver.find_element_by_id('down')
driver.switch_to.frame(frame)

And as @match said you could use id to search elements. But it works also with your xpath

import selenium.webdriver

url = 'http://cafe.daum.net/WekiMeki'

driver = selenium.webdriver.Chrome()
#driver = selenium.webdriver.Firefox()
driver.get(url)

frame = driver.find_element_by_id('down')
driver.switch_to.frame(frame)

driver.find_element_by_id('fancafe-widget-cheer').click()

#driver.find_element_by_xpath('/html/body/div[1]/div[5]/div[1]/div[3]/div/div/section/div/a').click()

Upvotes: 4

match
match

Reputation: 11070

Using an absolute path like that is risky, since if the site structure changes, it won't work anymore.

Luckily the element you want to click has a unique id: id="fancafe-widget-cheer"

So you can select it by doing:

driver.find_element_by_id('fancafe-widget-cheer')

Upvotes: 2

Related Questions