Home > OS >  Getting the element on page source but unable to locate using xpath
Getting the element on page source but unable to locate using xpath

Time:09-27

I am trying to find the element by using xpath but they are unable to locate. While when I am getting the page source using selenium they have the element and Also I have checked but the element are not in Iframe.

Here is my code:

from requests_html import HTMLSession
import pandas as pd
from fake_useragent import UserAgent
from requests_html import AsyncHTMLSession
from selenium import webdriver
from shutil import which
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

ua = UserAgent()
s = HTMLSession()
asession = AsyncHTMLSession()

url = 'https://ordiamond-frame-categoryembed-catid23621.jewelershowcase.com/search/results?query=124405'
try : User_Agent = str(ua.chrome)
except : pass 
headers = {'User-Agent':User_Agent}
response = s.get(url, headers= headers)
print(response)

link = response.html.xpath('//a[@class="image logClick containerFix"]/@href')

if link:

    p_url = "https://ordiamond-frame-categoryembed-catid23621.jewelershowcase.com"   (link[0])
    chrome_path = which('chromedriver')
    driver = webdriver.Chrome(executable_path=chrome_path)
    driver.maximize_window()
    driver.get(p_url)
    time.sleep(20)
    with open('data.html', 'w') as file:
        file.write(str(driver.page_source))
    print(driver.page_source)
    driver.page_source
    WebDriverWait(driver, 50).until(EC.visibility_of_element_located((By.XPATH, '(//h3[@class="description"])[2]')))

    # time.sleep(16)
    na = driver.find_element_by_xpath('(//h3[@class="description"])[2]')
    print(na.text)

Hoping to get the solution. Thanks

CodePudding user response:

Consider the fact that you want the xpath of the link of the ring, here it is:

link = response.html.xpath('//*[@id='results']//a[1]')

CodePudding user response:

If there are multiple matching nodes, Selenium will always fetch the first set if we are using find_element not find_elements. also same with webdriverwait.

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(30)
wait = WebDriverWait(driver, 30)
driver.get("https://ordiamond-frame-categoryembed-catid23621.jewelershowcase.com/search/results?query=124405")
product = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='results']/descendant::a")))
product.click()
heading = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h3[@class='description']")))
print(heading.text)

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC4

Output :

14K Yellow 9x7 mm Oval Engagement Ring Mounting
  • Related