Home > database >  Searching text and then obtaining href from different attribute?
Searching text and then obtaining href from different attribute?

Time:06-15

So I'm trying to build a web scraper that fetches the respective links of certain Oculus Quest games based on title.

The problem I have is, I have managed to fetch elements containing my game title, but is unable to get the href associated with the for it.

Game title is stored in

<div >The Last Clockwinder</div>

but href is in

<a data-testid="4837365566303714" style="background-image: url(&quot;https://scontent.oculuscdn.com/v/t64.5771-25/39001658_516095626858884_984441088118681220_n.jpg?stp=dst-jpg_q92_s720x720&amp;_nc_cat=1&amp;ccb=1-7&amp;_nc_sid=79b88e&amp;_nc_ohc=jjv9rydHRUoAX_ET7qs&amp;_nc_ht=scontent.oculuscdn.com&amp;oh=00_AT_ZqI3vJRQiSWfhQGqQ-Y0rS8PcVYjpdUkTuXL-BTMa9g&amp;oe=62ADA8BB&quot;);" tabindex="1"  href="/experiences/quest/4837365566303714"><div ><div ></div></div></a>

Reference of picture would be useful here, attached below. Source: https://www.oculus.com/experiences/quest/section/1888816384764129

Here is my code

submit = driver.find_element(by=By.XPATH, value="//a[@class='store-section-item-tile']/following-sibling::div[contains(text(), '"   game_title   "')]")
print(submit.get_attribute("href"))

my idea had been to find the <a class... then check if <div contains my title.

Could anyone help out with this?

CodePudding user response:

Use the class name, all href of game names have common class name.

contents = driver.find_elements(By.CLASS_NAME, 'store-section-item-tile')

hrefs = dict()

for content in contents:
    hrefs[content] = content.get_attribute('href')

CodePudding user response:

submit=driver.find_element(By.XPATH,"")

This checks if an element contains a div class with that text

//div[@class='store-section-item' and .//div[@class='store-section-item__meta-name' and contains(text(),'The Last Clockwinder')]]/a[@class='store-section-item-tile']

This goes from the div tag and goes upwards to the ancestor aka the parent tag with that tag

//div[@class='store-section-item__meta-name' and contains(text(),'The Last Clockwinder')]/ancestor::div[@class='store-section-item']/a[@class='store-section-item-tile']

You can either use ancestor or .// inside the element you are searching.

  • Related