JavaScript
x
6
1
"problem lines"
2
for_tariff_loop = driver.find_elements_by_xpath("//span[@class='phx-radio__element']")
3
radio_label_list = for_tariff_loop[i].find_element_by_css_selector('span[class="phx-radio__label"]')
4
print(radio_label_list)
5
time.sleep(1)
6
website I’m scraping https://www.telekom.de/unterwegs/apple/apple-iphone-13-pro/graphit-512gb
label image I was not able to print the radio buttons label according to checked button. I don’t know what is the mistake and where I did it. could anyone help on this. It will be helpful for me to learn. Change tariff links given below links,
JavaScript
1
109
109
1
import xlwt
2
from selenium import webdriver
3
import re
4
import time
5
from datetime import date
6
from selenium.webdriver.support.ui import WebDriverWait
7
from selenium.webdriver.support import expected_conditions as EC
8
from selenium.webdriver.common.by import By
9
from selenium.webdriver.common.action_chains import ActionChains
10
from selenium.webdriver.common.keys import Keys
11
12
class telekommobiles:
13
def __init__(self):
14
self.url="https://www.telekom.de/mobilfunk/geraete/smartphone?page=1&pageFilter=promotion"
15
self.country='DE'
16
self.currency='GBP'
17
self.VAT='Included'
18
self.shipping = 'free shipping within 3-4 weeks'
19
self.Pre_PromotionPrice ='N/A'
20
self.color ='N/A'
21
def telekom(self):
22
#try:
23
driver=webdriver.Chrome()
24
driver.maximize_window()
25
driver.get(self.url)
26
today = date.today()
27
time.sleep(5)
28
cookies = driver.find_element_by_css_selector('button.cl-btn.cl-btn--accept-all').click()
29
print("cookies accepted")
30
links_prod_check = []
31
prod_models = []
32
prod_manufacturer =[]
33
prod_memorys = []
34
product_colors =[]
35
product_price_monthly_payments = []
36
product_price_one_time_payments =[]
37
product_links = []
38
containers = driver.find_elements_by_css_selector('div[class="styles_item__12Aw4"]')
39
i = 1
40
for container in containers:
41
p_links =container.find_element_by_tag_name('a').get_attribute('href')
42
i = i + 1
43
product_links.append(p_links)
44
#print(p_links)
45
for links in product_links:
46
driver.get(links)
47
#time.sleep(5)
48
49
#print(driver.current_url)
50
#links_prod_check.append(driver.current_url)
51
52
coloroptions = WebDriverWait(driver, 30).until(EC.presence_of_all_elements_located((By.XPATH,"//li[@data-qa='list_ColorVariant']")))
53
#print(coloroptions)
54
for i in range(len(coloroptions)):
55
coloroption = driver.find_elements_by_xpath("//li[@data-qa='list_ColorVariant']")
56
coloroption[i].click()
57
#print(coloroption[i])
58
time.sleep(3)
59
60
memoryoptions = WebDriverWait(driver, 30).until(EC.presence_of_all_elements_located((By.XPATH,"//span[@class='phx-radio__element']")))
61
for i in range(len(memoryoptions)):
62
memoryoption = driver.find_elements_by_xpath("//span[@class='phx-radio__element']")
63
try:
64
memoryoption[i].click()
65
except:
66
pass
67
68
time.sleep(5)
69
change_traiff = driver.find_element_by_css_selector('button[class="phx-link phx-list-of-links__link js-mod tracking-added"]').click()
70
time.sleep(3)
71
#looping for each section
72
section_loops = driver.find_elements_by_css_selector('section[class="tariff-catalog--layer"]')
73
#print(len(section_loops))
74
for section_loop in section_loops:
75
#print(section_loop)
76
time.sleep(5)
77
#Headings
78
heading_1 = section_loop.find_element_by_css_selector('h2[class="page-title page-title--lowercase"]').text
79
print(heading_1)
80
# looping for each separate boxes
81
each_box_subcontainers = section_loop.find_elements_by_css_selector('.phx-tariff-box__section')
82
#print(len(each_box_subcontainers))
83
for subcontainer in each_box_subcontainers:
84
#print(subcontainer)
85
looping_for_tariff = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH,"//span[@class='phx-radio__element']")))
86
#print(looping_for_tariff)
87
for i in range(len(looping_for_tariff)):
88
#print(i)
89
try:
90
for_tariff_loop = driver.find_elements_by_xpath("//span[@class='phx-radio__element']")
91
for_tariff_loop[i].click()
92
time.sleep(3)
93
except:
94
pass
95
96
for_tariff_loop = driver.find_elements_by_xpath("//span[@class='phx-radio__element']")
97
radio_label_list = for_tariff_loop[i].find_element_by_css_selector('span[class="phx-radio__label"]')
98
print(radio_label_list)
99
time.sleep(1)
100
101
102
103
104
change_traiff_close_button = driver.find_element_by_css_selector('span[class="icon-after-yellow-close right close popup-close-tr js-popup-close"]').click()
105
106
107
telekom_de=telekommobiles()
108
telekom_de.telekom()
109
Advertisement
Answer
You are trying to find element within an element. Finding radio_label_list
using for_tariff_loop[i]
, xpath for radio_label_list
will become like below:
JavaScript
1
2
1
//span[@class='phx-radio__element']//span[@class="phx-radio__label"]
2
Which does not exist in the DOM.
I tried the last part of the code. And was able to print the Memory size like below. Do try and confirm:
Replaced css-selector
for radio_label_list
with this xpath ./following-sibling::span
JavaScript
1
16
16
1
looping_for_tariff = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//span[@class='phx-radio__element']")))
2
# print(looping_for_tariff)
3
for i in range(len(looping_for_tariff)):
4
# print(i)
5
try:
6
for_tariff_loop = driver.find_elements_by_xpath("//span[@class='phx-radio__element']")
7
for_tariff_loop[i].click()
8
time.sleep(3)
9
except:
10
pass
11
12
for_tariff_loop = driver.find_elements_by_xpath("//span[@class='phx-radio__element']")
13
radio_label_list = for_tariff_loop[i].find_element_by_xpath("./following-sibling::span").text
14
print(radio_label_list)
15
time.sleep(1)
16
As per the comments, check this code:
JavaScript
1
32
32
1
driver.get("https://www.telekom.de/unterwegs/apple/apple-iphone-13-pro/graphit-512gb")
2
wait = WebDriverWait(driver,30)
3
wait.until(EC.element_to_be_clickable((By.XPATH,"//button[text()='Accept All']"))).click()
4
5
wait.until(EC.element_to_be_clickable((By.XPATH,"//ul[contains(@class,'phx-tariff-notification-box-new__element--desktop-tablet')]/li[2]/button"))).click()
6
7
length = len(driver.find_elements_by_class_name("phx-tariff-box__section"))
8
9
for i in range(length):
10
print("----------------------------------------------------------------------------------------------------------")
11
options = driver.find_elements_by_class_name("phx-tariff-box__section")
12
datas = options[i].find_element_by_xpath(".//div[contains(@class,'phx-tariff-box__volume')]").get_attribute("innerText")
13
print("data: {}".format(datas))
14
len_types = len(options[i].find_elements_by_xpath(".//div[@class='phx-tariff-box__radios-inner']//label"))
15
types = options[i].find_elements_by_xpath(".//div[@class='phx-tariff-box__radios-inner']//label")
16
if len(types) == 0:
17
price = options[i].find_element_by_xpath(".//p[@data-qa='block_TariffPrice']").get_attribute("innerText")
18
print(price)
19
else:
20
for j in range(len_types):
21
types[j].click()
22
time.sleep(2)
23
options = driver.find_elements_by_class_name("phx-tariff-box__section")
24
types = options[i].find_elements_by_xpath(".//div[@class='phx-tariff-box__radios-inner']//label")
25
try:
26
types[j].find_element_by_xpath("./input[@checked]")
27
type = types[j].find_element_by_xpath("./span[2]").get_attribute("innerText")
28
price = options[i].find_element_by_xpath(".//p[@data-qa='block_TariffPrice']").get_attribute("innerText")
29
print(f"{type}: {price}")
30
except:
31
pass
32