How to get the xpath of elements inside a div using python selenium

Question:

<div class="col-md-6 profile-card_col">
    <span class="label">نام شرکت :</span> 
    <p class="value">اسپاد فولاد آسیا</p>
</div>

Its been a while I have been trying to fix this problem but what appears to people think I’m looking for is to get the value in the <span> and <p> but I’m trying to get the xpath of both using python selenium the challenge is that I do not know anything about the <span> and <p> and the code above is just a example. So my main goal to get the xpath of both the <span> and <p> only using the the main div xpath.

Asked By: kevinjonson133

||

Answers:

in chrome:
You can open the chrome developer console and right click on the tag you want and then copy the sub menu and then click on an option called Xpath. Xpath will copy the tag you want for you.

Answered By: emanuel

You can try the below XPath:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

span_value = driver.find_element(By.XPATH, ".//*[@class='col-md-6 profile-card_col']/span").text

paragraph_value = driver.find_element(By.XPATH, ".//*[@class='col-md-6 profile-card_col']/p").text

Next time, post the URL and the full code you’ve tried and explain your issue clearly and briefly.

Answered By: AbiSaran
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.