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

Time:12-25

<div >
    <span >نام شرکت :</span> 
    <p >اسپاد فولاد آسیا</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.

CodePudding user response:

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.

CodePudding user response:

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.

  • Related