Reputation: 185
I have the below element on a webpage I am trying to scrape.
<a href="http://www.mylink/?p=20391">Sup Bonds Result June 26, 2018</a>
Below is the code that I am trying to use but doesn't seem to be working although the value exists. It worked for initial few instances but then isn't providing any result.
try:
element=driver.find_element_by_partial_text('Sup Bonds Result June 26, 2018')
except NoSuchElementException:
driver.quit()
below is the error that i Received, I have already used the time.sleep o allow sometime for the script to locate the element.
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Sup Bonds Result June 26, 2018"}
Any help is appreciated.
Upvotes: 1
Views: 299
Reputation: 193308
Seems you were pretty close. As you are simply trying to locate (not clicking) the element, you need to induce WebDriverWait for the visibility of the element as follows:
try:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.PARTIAL_LINK_TEXT, "Sup Bonds Result June 26, 2018"))).click()
except TimeoutException:
driver.quit()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
Upvotes: 1
Reputation: 52685
Try to apply ExplicitWait as below:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
try:
wait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Sup Bonds Result June 26, 2018")))
except TimeoutException:
driver.quit()
Also note that if you want to use search by link text (partial link text) you need to pass text exactly as it appears on page in browser, but not as it appears in page source. So if it looks on page like "SUP BONDS..."
you need to use the same in code
Upvotes: 1