1

I'm trying to use python and selenium to select a specific radio button out of a few that live within a web page. Luckily, the radio button that I want to select does have specific text associated, which should make it easier for me to select. Unfortunately, I'm missing something since all attempts have errored out. Below is the HTML code, what i've tried, and what i'm trying to achieve.

HTML

    <div data-a-input-name="slotsRadioGroup" class="a-radio a-radio-fancy slotButtonRadio"><label><input type="radio" name="slotsRadioGroup" value="" autocomplete="off"><i class="a-icon a-icon-radio"></i><span class="a-label a-radio-label">
        <span class="slotRadioLabel">
            5PM - 7PM
        </span>

    <div class="a-row">
        <span class="a-size-small slot-button-micro-copy preselected-visible">
            Soonest available time with all items.
        </span>
    </div>


    </span></label></div>

</div></div>

The radio button I need to select will always have the text "Soonest available..." present.

What I've tried

I've tried different variations of xpath code to attempt to select this based on the text. My last attempt was:

driver.find_element_by_xpath("//input[@type='radio']and following-sibling::span[contains(., 'Soonest available')").click()

Which results in the following error:

SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//input[@type='radio']and following-sibling::span[contains(., 'Soonest available')' is not a valid XPath expression.

Expected Result What I want is to be able to select the specific radio button based on the above text being present.

3
  • can you share the link? Commented Apr 16, 2020 at 0:07
  • Since no link can't test it. See if this helps: stackoverflow.com/questions/35470171/… Commented Apr 16, 2020 at 0:10
  • So, it's a little difficult to provide an accurate URL. The above is meant to find an element that only exists if an available shipping slot was found on Amazon Fresh. Given the circumstances, that's few and far between. This is, however, where the URL should lead to when that text is available: amazon.com/gp/buy/shipoptionselect/handlers/… Commented Apr 16, 2020 at 15:29

2 Answers 2

1

To Select Radio button based on text Induce WebDriverWait and wait for element_to_be_clickable() and following xpath options.

XPATH 1:

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//label[.//span[contains(.,'Soonest available time with all items')]]/input[@name='slotsRadioGroup']"))).click()

XPATH 2:

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//span[contains(.,'Soonest available time with all items')]/preceding-sibling::input[1]"))).click()

If Web driver click not work then you can try javascripts executor to click the same.

radiobtn=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//label[.//span[contains(.,'Soonest available time with all items')]]/input[@name='slotsRadioGroup']")))

driver.execute_script("arguments[0].click();", radiobtn)

Note:- You need to import following libraries.

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

Comments

0

Try this:

for i in driver.find_elements_by_tag_name("span"):
    if "Soonest available" in i.text:
        result = i
        break

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.