Home > Back-end >  Selenium Python Getting Dynamic Link Text
Selenium Python Getting Dynamic Link Text

Time:09-17

I am trying to get the link text from a website link and said text changes. I can't even find the element. I have tried by xpath, css selector, and tag name to no avail.

There is no IFRAME and I have given time to load the elements.

<card type="CCCC" ng-repeat="trip in tripsVM.tripsByType.SA | orderBy: 'FSD' | limitTo: tripsVM.limit" on-cancel="tripsVM.openCancelModal(trip)" on-CChic="tripsVM.gocchicppp)" on-details="tripsVM.openDetails(trip.ppp)" on-edittrip="tripsVM.editTrip(trip)" on-emailtrip="tripsVM.emailPNR(trip)" on-notificationtrip="tripsVM.createN(trip)" on-pt="tripsVM.goPy(trip)" on-ressiuetrip="tripsVM.goR(trip)" structure="trip"><!---->
<!----><div class="card desktopView tripDescFonts" data-ng-if="cardVM.breakpoint === 'desktop'">
<div class="row">
<div class="data ppp-description" ng-class="{'empty-space':!cardVM.pppDescription}">
<a href="" ng-click="cardVM.onDetails()">**NEEDED TEXT**</a>

CodePudding user response:

Use BeautifulSoup

from bs4 import BeautifulSoup
html = """<card type="CCCC" ng-repeat="trip in tripsVM.tripsByType.SA | orderBy: 'FSD' | limitTo: tripsVM.limit" on-cancel="tripsVM.openCancelModal(trip)" on-CChic="tripsVM.gocchicppp)" on-details="tripsVM.openDetails(trip.ppp)" on-edittrip="tripsVM.editTrip(trip)" on-emailtrip="tripsVM.emailPNR(trip)" on-notificationtrip="tripsVM.createN(trip)" on-pt="tripsVM.goPy(trip)" on-ressiuetrip="tripsVM.goR(trip)" structure="trip"><!---->
<!----><div class="card desktopView tripDescFonts" data-ng-if="cardVM.breakpoint === 'desktop'">
<div class="row">
<div class="data ppp-description" ng-class="{'empty-space':!cardVM.pppDescription}">
<a href="" ng-click="cardVM.onDetails()">**NEEDED TEXT**</a>"""

soup = BeautifulSoup(html)
soup.find('a', href=True).text # -> '**NEEDED TEXT**'

CodePudding user response:

You can try with Explicit waits and get_attribute to get the **NEEDED TEXT*.

a_tag = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@ng-click='cardVM.onDetails()']")))
print(a_tag.get_attribute('innerHTML'))

You'll need Imports as well.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
  • Related