Reputation: 75
I have the following code on a webpage:
<label data-correct="false">
<input type="radio" ng-model="quizCtrl.state.selectedOptionForCurrentQuestion" ng-value="option" name="q0" class="ng-pristine ng-untouched ng-valid ng-empty" value="[object Object]">
<!--option image-->
<!---->
<!--option text-->
<span ng-bind-html="quizCtrl.trustAsHtml(option.text)"><p>Earned three college degrees and supported himself as a small businessman - Google values entrepreneurship.</p></span>
</label>
<label data-correct="true">
<input type="radio" ng-model="quizCtrl.state.selectedOptionForCurrentQuestion" ng-value="option" name="q0" class="ng-pristine ng-untouched ng-valid ng-empty" value="[object Object]">
<!--option image-->
<!---->
<!--option text-->
<span ng-bind-html="quizCtrl.trustAsHtml(option.text)"><p>Has several years of work experience, and enjoys volunteering for Habitat for Humanity - Google values these character traits.</p></span>
</label>
<label data-correct="false">
<input type="radio" ng-model="quizCtrl.state.selectedOptionForCurrentQuestion" ng-value="option" name="q0" class="ng-pristine ng-untouched ng-valid ng-empty" value="[object Object]">
<!--option image-->
<!---->
<!--option text-->
<span ng-bind-html="quizCtrl.trustAsHtml(option.text)"><p>Recently graduated from Harvard University - Google is known for hiring the best and the brightest.</p></span>
</label>
<label data-correct="false">
<input type="radio" ng-model="quizCtrl.state.selectedOptionForCurrentQuestion" ng-value="option" name="q0" class="ng-pristine ng-untouched ng-valid ng-empty" value="[object Object]">
<!--option image-->
<!---->
<!--option text-->
<span ng-bind-html="quizCtrl.trustAsHtml(option.text)"><p>Worked as an independent contractor in different areas of the computer industry - Google values versatility.</p></span>
</label>
I want to target the data-correct field and if it's true the program should click it. This is what I have so far:
from selenium import webdriver
driver = webdriver.Chrome('chromedriver.exe')
driver.get(url)
driver.find_elements_by_tag_name('label')
I'm quite new to python and selenium so I'm not sure what to do next
Upvotes: 0
Views: 42
Reputation: 19929
data = [ i.get_attribute("data-correct") for i in driver.find_elements_by_tag_name('label')]
data-correct is an attribute of the label element so you can use get_attribute given you are able locate the element correctly
if you are not concerned in getting the tag but about clicking it then use xpath instead
[ i.click for i in driver.find_elements_by_xpath('//label[@data-correct="true"]')]
but if clicking the element will cause stale element then you have to refind all elements .
use instead:
elems = driver.find_elements_by_xpath('//label[@data-correct="true"]')
count = 0
while(count!=len(elems)):
elems = WebDriverWait(driver,15).until(EC.presence_of_all_elements_located(By.xpath,'//label[@data-correct="true"]'))
elems[count].click()
count+=1;
use webdriver wait if you have to :
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
Upvotes: 1