IE404
IE404

Reputation: 11

Python Selenium - find table elements <td> based on a string and click the associatet radio button

I'm trying to select a radiobutton, based on his text value.

So on this site i skip the first page with this code (because its a frame)

driver.switch_to_frame(0)
elem = driver.find_element_by_id("bedin")
elem.click()
clickWeiter()

After that i want to click a radiobutton, which has a certain text, for example 'Bus'

My Problem is, that the actual radio button is in a tag

<input type="radio" name="mittel" value="2" onclick="zweiSelect4('neu',2);document.form1.linie.disabled=false;">

but the readable text 'Bus' is in a td tag

<td height="36" align="left" class="Stil1"><input type="radio" name="mittel" value="2" onclick="zweiSelect4('neu',2);document.form1.linie.disabled=false;">Bus </td>

so far i managed to click the radio button based on his value, not text

 driver.find_element_by_css_selector("input[type='radio'][value='2']").click

or i managed to get the text, but couldnt click it

list = driver.find_elements_by_tag_name("td")
for i in list:
    searchval =("Bus")
if searchval in str(i.text):
    print(" BULLSEYE" , i.text)
    i.click()

my goal is to have a function which i pass the desired text (Sonstiges, Bus,etc) and then click the element.

Upvotes: 0

Views: 5103

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193308

As per the website http://nvv31.webqms.de/nvv/kunden/ to click any of the radio buttons associated with text as Regionalzug, Tram or Bus you can write a function which will take the string as an argument as follows:

def click_me(myString):
    driver.find_element_by_xpath("//table[@class='Stil1']//td[contains(.,'" + myString + "')]/input[@name='mittel']").click()

Now you can call the function click_me() with any of the relevant text as follows:

click_me("Regionalzug")
click_me("Tram")
click_me("Bus")
click_me("AnrufSammelTaxi")

Upvotes: 2

cruisepandey
cruisepandey

Reputation: 29382

You can use this xpath :

//td[contains(text(),'Bus')]/input  

in code :

driver.find_element_by_xpath("//td[contains(text(),'Bus')]/input").click()  

as you have mentioned, that you would like to have a method , for that you can try :

visible = "Bus"
driver.find_element_by_xpath("//td[contains(text(),'"+visible+"')]/input").click()  

Method :

def myfunc(visible):
 driver.find_element_by_xpath("//td[contains(text(),'"+visible+"')]/input").click()  

Then you can call this method with your required text.

Hope this will help.

UPDATE :

Those are text nodes, you can try something like :

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.action_chains import ActionChains

driver   = webdriver.Chrome(executable_path = r'C:/chromedriver_win32/chromedriver.exe')
driver.maximize_window()

wait = WebDriverWait(driver,40)

driver.get("http://nvv31.webqms.de/nvv/kunden/")

driver.switch_to.frame(driver.find_element_by_xpath("//frame[@src='kundengarantie.php']"))
driver.find_element_by_name('bedin').click()
continue_link = driver.find_element_by_link_text('weiter')
continue_link.click()

driver.switch_to.default_content()

driver.switch_to.frame(driver.find_element_by_xpath("//frame[@src='kundengarantie.php']"))

all_text = driver.find_elements_by_css_selector("td.Stil1 input") 

text_node = driver.execute_script("return arguments[0].nextSibling.textContent;", all_text[1])

text_node_trim = text_node.strip()

print(text_node_trim)


def myfunc(visible):
 driver.find_element_by_xpath("//td[contains(.,'"+text_node_trim+"')]/input").click()  

myfunc("Haltestelle / Bahnhof")

Upvotes: 0

Related Questions