0

I was wondering how I can click on the button on this page using Python Selenium.

Here is the webpage:

https://hk.news.yahoo.com/poll/f83b2360-b41c-11e9-8d6b-b77be6568e98

The source code for the button is:

<label class="option Cur(p) Ov(h) D(b) Pos(r) Bxsh($BorderLikeBoxShadow) Bdc($borderGray) Px(15px) Py(23px)" data-reactid="43">
 <div class="Pos(r) D(tb)" data-reactid="44">
  <div class="D(tbc) Ta(c) Va(m)" data-reactid="45"><input type="radio" value="o0" class="D(n) check" name="option" data-reactid="46"><img class="Trsdu(.3s) Op(0.7) Fil($opacityMsOld) Mend(15px) W(26px) H(26px)" src="https://s.yimg.com/ji/news/ybrain/[email protected]" alt="" data-reactid="47"></div>
  <div class="D(tbc) Va(m) Lh(1.5) C($c-fuji-grey-i) LineClamp(1) Fz(16px)" data-reactid="48">支持</div>
 </div>
</label>

I tried a script like below but it didn't work.

import os
import glob
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import WebDriverException

options = webdriver.ChromeOptions()
options.add_argument('window-size=1920x1080')

link_url="https://hk.news.yahoo.com/poll/f83b2360-b41c-11e9-8d6b-b77be6568e98"

driver = webdriver.Chrome('chromedriver', chrome_options=options)
driver.get(link_url)

driver.find_elements_by_xpath("//*[@type='radio']")[0].click()

Any suggestions? thanks!

1 Answer 1

1

Use WebdriverWait and javaScript Executor to click on the element using below xpath.

element=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"(//label[contains(@class,'option Cur(p) Ov(h) D(b) Pos(r)')]//div[contains(@class,'D(tbc) Va(m)')])[1]")))
driver.execute_script("arguments[0].click();",element)

OR

element=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"(//label[contains(@class,'option Cur(p) Ov(h) D(b) Pos(r)')]//img)[1]")))
driver.execute_script("arguments[0].click();",element)

You need to import followings.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.