0

Let's say this website has frequently changed data (stores). I am also only interested in certain stores from my list. How do I get all the data of the first store from the top and from my list?

I was only able to get the entire list of stores

Code:

from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://www.justdial.com/Bangalore/Bakeries')

name = ["Pat-a-Cake", "Chef Bakers"]

storenames = driver.find_elements(By.CLASS_NAME, "store-name")
for i in storenames:
   rest = driver.find_element(By.CLASS_NAME, "store-name")
   print(i.text)
2
  • The target site is not accessible. It will be hard for people to deduce the DOM. Additionally, you question is not clear: "all the data from the first store from the top AND from the list?" What "top" means here? Top in the visibility or the first to appear in the DOM? They can be different. Jun 20 at 23:06
  • Hello! I meant "first appeared" on the page. This website works great for me....I made this example specifically for this website. I found a solution how to make a condition for choosing a store with a specific name. Here it is: name = ["Pat-a-Cake", "Chef Bakers"] storenames = driver.find_elements(By.CLASS_NAME, "store-name") for i in storenames: if ('Pat-a-Cake44') in i.text: print(i.text) But how do I use a list (more than one store name)?
    – Synthezx
    Jun 21 at 10:00

1 Answer 1

0

This is what I tried with Java + Selenium and it sort of worked:

System.out.println(driver.findElement(By.xpath("//li[@class='cntanr'][1]//h2//span[@class='lng_cont_name']")).getText());
System.out.println(driver.findElement(By.xpath("//li[@class='cntanr'][1]//p[@class='contact-info ']")).getText());
System.out.println(driver.findElement(By.xpath("//li[@class='cntanr'][1]//p[@class='address-info adinfoex ']")).getText());
System.out.println();
System.out.println(driver.findElement(By.xpath("//li[@data-cn='Pat-a-Cake']//h2//span[@class='lng_cont_name']")).getText());
System.out.println(driver.findElement(By.xpath("//li[@data-cn='Pat-a-Cake']//p[@class='contact-info ']")).getText());
System.out.println(driver.findElement(By.xpath("//li[@data-cn='Pat-a-Cake']//p[@class='address-info adinfoex ']")).getText());
System.out.println();
System.out.println(driver.findElement(By.xpath("//li[@data-cn='Chef Bakers']//h2//span[@class='lng_cont_name']")).getText());
System.out.println(driver.findElement(By.xpath("//li[@data-cn='Chef Bakers']//p[@class='contact-info ']")).getText());
System.out.println(driver.findElement(By.xpath("//li[@data-cn='Chef Bakers']//p[@class='address-info adinfoex ']")).getText());

Although, the phone number isn't showing in the output because it's not a text from and sort of custom CSS based icon.

So maybe you can work around this in some way to get the desired output using Python by adding your logic and loops.

3
  • Thanks for your comment! Unfortunately, i'm not good in java:((
    – Synthezx
    Jun 21 at 10:02
  • I partially solved my issue. Here is the code: code name = ["Pat-a-Cake", "Chef Bakers", "Cake and Bake Emporium"] storenames = driver.find_elements(By.CLASS_NAME, "store-name") for i in storenames: if any(x in i.text for x in name): print(i.text) else: print ('NO')code. But, now I get a result where many are NO and these three stores are from the list. How do I get only one result - the topmost store? Thank you!
    – Synthezx
    Jun 21 at 10:16
  • Check Python equivalent of these: driver.findElement(By.xpath("//li[@class='cntanr'][1]//h2//span[@class='lng_cont_name']")).getText() driver.findElement(By.xpath("//li[@class='cntanr'][1]//p[@class='contact-info ']")).getText() driver.findElement(By.xpath("//li[@class='cntanr'][1]//p[@class='address-info adinfoex ']")).getText() Jun 21 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.