paniks paniks - 9 months ago 68
HTML Question

Selenium Find Sub-Child href Element

I'm trying to click the following link using selenium.

<div id="RECORD_2" class="search-results-item">
<a hasautosubmit="true" oncontextmenu="javascript:return IsAllowedRightClick(this);" class="smallV110" href="#;cacheurlFromRightClick=no"></a>

Which record to click is not known before the code is executed. Record_2 has multiple children, and the one included is the one I want to click. The link is edited for the sake of privacy. I tried to do something like that where name is the record variable, however it doesn't work.

driver.find_element_by_css_selector("css=div#"RECORD_%s" % (name).smallV110")

I'm a complete newbie to selenium so I couldn't figure out a way to sort this out. I would appreciate any help. Thanks!


Note that this is not Selenium IDE and you don't need the css= at the beginning of a selector.

There are multiple ways to locate the link element, e.g.:

driver.find_element_by_css_selector(".search-results-item a.smallV110")
driver.find_element_by_css_selector("[id^=RECORD] a.smallV110")  # id starts with "RECORD"

If you know the id value beforehand:

id_i_know = 2
driver.find_element_by_css_selector("[id=RECORD_%d] a.smallV110" % id_i_know)

You don't have to have that smallV110 class attribute check - I've added it to increase chances of not matching other a elements inside the div (not sure what they are, you have not posted the entire HTML).