Home > OS >  problem in clicking radio button can't able to select a radio button
problem in clicking radio button can't able to select a radio button

Time:10-09

I'm trying to scrape a data from this website https://www.telekom.de/unterwegs/apple/apple-iphone-13-pro/sierrablau-128gb.

I'm facing issue with process please anyone help me on this.

tariff page details change tariff pic

I am trying to select from a list of 4 radio buttons, but can't find a way to select them. Below is the code I am working with. But I would like to select Choice one by one and need to scrape data by looping for the 4 box. (To click it) How do I do this? please anyone help on this.

picture of radio button selector

import xlwt
from selenium import webdriver
import re
import time
from datetime import date
class telekommobiles:
    def __init__(self):
        self.url="https://www.telekom.de/mobilfunk/geraete/smartphone?page=1&pageFilter=promotion"
        self.country='DE'
        self.currency='GBP'
        self.VAT='Included'
        self.shipping = 'free shipping within 3-4 weeks'
        self.Pre_PromotionPrice ='N/A'
        self.color ='N/A'
    def telekom(self):
        #try:
            driver=webdriver.Chrome()  
            driver.maximize_window()          
            driver.get(self.url)
            today = date.today()
            time.sleep(5)
            cookies = driver.find_element_by_css_selector('button.cl-btn.cl-btn--accept-all').click()
            print("cookies accepted")            
            links_prod_check = []
            prod_models = []
            prod_manufacturer =[]
            prod_memorys = []
            product_colors =[]
            product_price_monthly_payments = []
            product_price_one_time_payments =[]
            product_links = []
            containers = driver.find_elements_by_css_selector('div[class="styles_item__12Aw4"]')
            i = 1            
            for container in containers:
                p_links =container.find_element_by_tag_name('a').get_attribute('href')
                i = i   1
                product_links.append(p_links)
                #print(p_links)
            for links in product_links:
                driver.get(links)
                time.sleep(5)
                #print(driver.current_url)
                #links_prod_check.append(driver.current_url)

                change_traiff = driver.find_element_by_css_selector('button[class="phx-link phx-list-of-links__link js-mod tracking-added"]').click()
                time.sleep(5)
                heading_1 = driver.find_element_by_css_selector('h2[class="page-title page-title--lowercase"]').text
                print(heading_1)
                tariff = driver.find_elements_by_css_selector('div[class="phx-tariff-box phx-tariff-box--inside_tariff-tile "]')
                for loop_tariff in tariff:

                        #mit_smartphone = loop_tariff.find_element_by_css_selector('input[id="familyId-tarfamgr10002-fmlt100010-0"]').click()
                        magenta_s = loop_tariff.find_element_by_css_selector('p[class="phx-t6 phx-t--medium"]').text
                        print(magenta_s)
                        mit_smartphone_top = loop_tariff.find_element_by_css_selector('span[class="phx-radio__label"]').text
                        print(mit_smartphone_top)
                        magenta_s_price = loop_tariff.find_element_by_css_selector('span[class="phx-price__value "]').text
                        print(magenta_s_price)

                        mit_smartphone1 = loop_tariff.find_element_by_id('familyId-tarfamgr10002-fmlt420001-0').click()
                        magenta_s_GB1 = loop_tariff.find_element_by_css_selector('p[class="phx-price phx-price--size_large phx-price--strong phx-price--color_brand"]').text
                        print(magenta_s_GB1)
        #except:
            #pass
        
telekom_de=telekommobiles()
telekom_de.telekom()

CodePudding user response:

After selecting a different Option the page gets Refreshed, hence the issue. I was not able to find where you were trying to click on the buttons in your code. So tried to click on all the radio buttons with below code and was successful. Check the code once.

from selenium import webdriver
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(executable_path="path to chromedriver.exe")
driver.maximize_window()
driver.implicitly_wait(10)

driver.get("https://www.telekom.de/unterwegs/apple/apple-iphone-13-pro/sierrablau-128gb")
wait = WebDriverWait(driver,30)
wait.until(EC.element_to_be_clickable((By.XPATH,"//button[text()='Accept All']"))).click()

radiooptions = wait.until(EC.presence_of_all_elements_located((By.XPATH,"//span[@class='phx-radio__element']")))

for i in range(len(radiooptions)):
    radiooptions = driver.find_elements_by_xpath("//span[@class='phx-radio__element']")
    radiooptions[i].click()
    time.sleep(2)

CodePudding user response:

please li element instead of span

//li[@data-qa='list_ColorVariant']

and also add wait once you click on it. 5secs. then click the next one

  • Related