Read Description list in selenium python

Multi tool use


Read Description list in selenium python
How can I read text of <dd>
tag which has <dt>
like Commodity code.
<dd>
<dt>
`<dl class="dl">
<dt>Trading Screen Product Name</dt>
<dd>Biodiesel Futures (balmo)</dd>
<dt>Trading Screen Hub Name</dt>
<dd>Soybean Oil Pen 1st Line</dd>
<dt>Commodity Code</dt>
<dd><div>S25-S2Z</div></dd>
<dt>Contract Size</dt>
<dd><div>100 metric tonnes (220,462 pounds)</div></dd>
</dl>`
from selenium import webdriver
driver = webdriver.Chrome("C:\Python36-32\selenium\webdriver\chromedriver.exe")
link_list = ["http://www.theice.com/products/31500922","http://www.theice.com/products/243"]
driver.maximize_window()
for link in link_list:
driver.get(link)
desc_list = driver.find_elements_by_class_name("dl")
2 Answers
2
@Andersson is correct but the problem is he calls find_elements method in the place he supposed to call find element, please execute this code, it would work.
desc_list = driver.find_element_by_class_name("dl")
desc_list.find_element_by_xpath("./dt[.='Commodity Code']/following-sibling::dd").text
Please note I called find_element
in the first line not find_elements
.
find_element
find_elements
Try to implement below code to get the values of "Commodity Code"
as output:
"Commodity Code"
for desc_list in driver.find_elements_by_class_name("dl"):
print(desc_list.find_element_by_xpath("./dt[.='Commodity Code']/following-sibling::dd").text)
Yep, right. I didn't notice that
desc_list
is a list :) Check updated– Andersson
34 secs ago
desc_list
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
print(desc_list.find_element_by_xpath("./dt[.='Commodity Code']/following-sibling::dd").text) AttributeError: 'list' object has no attribute 'find_element_by_xpath'
– Anupam Soni
44 mins ago