selenium/python - fails to find partial link text

  • Last Update :
  • Techknowledgy :

Actually, there is no link text that contain WO20! I think you confuse link text with href attribute of <a> element. You can find target element by link text/partial link text with following code:

driver.find_element_by_link_text('WO/2015/102036')

or

driver.find_element_by_partial_link_text('WO/20')

this XPaths should be applicable also:

driver.find_element_by_xpath('//a[contains(@href, "WO20")]')
driver.find_element_by_xpath('//a[contains(text(), "WO/20")]')

Suggestion : 2

in Selenium The find_element_by_partial_link_text() method is used to locate links that match part of the link text in python.,If there are multiple matching partial link texts, only the first matching item is accessed.,After finding the link element with the partial link text() method on the actual site, we will click the link.,partial link text in selenium python & EXAMPLES

				driver.find_element_by_partial_link_text("substring of partial_link_text")
				<html>

				<head>
				   <title>Find Partial Link Text</title>
				</head>

				<body>
				   <a href="https://wikicode.tips">anchor text</a>
				</body>

				</html>
				from selenium
				import webdriver

				driver = webdriver.Chrome('chromedriver.exe')

				driver.find_element_by_partial_link_text("part of anchor text")
				from selenium
				import webdriver
				import time

				driver = webdriver.Chrome('chromedriver.exe')

				driver.get('https://wikicode.tips/selenium-xpath-containstext-python-example/')

				#
				if we don 't specify implicitly wait, error occur.
				# because we will find element too quickly before we find element.
				driver.implicitly_wait(2)

				text = driver.find_element_by_partial_link_text("db.Collection.insert()")

				time.sleep(1)
				text.click()

Suggestion : 3

If you want to locate several elements with the same attribute replace find_element with find_elements.,There are various strategies to locate elements in a page. You can use the most appropriate one for your case. Selenium provides the following method to locate elements in a page:,These examples cover some basics, but in order to learn more, the following references are recommended:,Use this when you know the link text used within an anchor tag. With this strategy, the first element with the link text matching the provided value will be returned. If no element has a matching link text attribute, a NoSuchElementException will be raised.

from selenium.webdriver.common.by
import By

driver.find_element(By.XPATH, '//button[text()="Some text"]')
driver.find_elements(By.XPATH, '//button')
ID = "id"
NAME = "name"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"
find_element(By.ID, "id")
find_element(By.NAME, "name")
find_element(By.XPATH, "xpath")
find_element(By.LINK_TEXT, "link text")
find_element(By.PARTIAL_LINK_TEXT, "partial link text")
find_element(By.TAG_NAME, "tag name")
find_element(By.CLASS_NAME, "class name")
find_element(By.CSS_SELECTOR, "css selector")
<html>

<body>
   <form id="loginForm">
      <input name="username" type="text" />
      <input name="password" type="password" />
      <input name="continue" type="submit" value="Login" />
   </form>
</body>

</html>
login_form = driver.find_element(By.ID, 'loginForm')
<html>

<body>
   <form id="loginForm">
      <input name="username" type="text" />
      <input name="password" type="password" />
      <input name="continue" type="submit" value="Login" />
      <input name="continue" type="button" value="Clear" />
   </form>
</body>

</html>

Suggestion : 4

When we try to find any element in an HTML page that does not exist, NoSuchElementException will be raised.,When we try to run the code below, it raises NoSuchElementException. This is because we try to find an element called userNam, but the webpage element name in the source code is userName.,An exception is an event that occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.,Script will terminate when an exception is raised. We can handle exceptions with the try except block and continue with the flow of the script.

When we try to find any element in an HTML page that does not exist, NoSuchElementException will be raised.

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {
   "method": "css selector",
   "selector": "[name="
   userNam "]"
}
from selenium
import webdriver
from selenium.webdriver.common.by
import By
from selenium.webdriver.support.ui
import WebDriverWait
from selenium.webdriver.support
import expected_conditions as EC
from selenium.webdriver.common.keys
import Keys
import time

PATH = r "provide your chrome driver path here"

driver = webdriver.Chrome(PATH)

driver.get("http://demo.guru99.com/test/newtours/")

user_name = driver.find_element_by_name("userNam")
password = driver.find_element_by_name("password")
submit = driver.find_element_by_name("submit")

user_name.send_keys("mercury")
password.send_keys("mercury")
submit.click()
from selenium
import webdriver
from selenium.webdriver.common.by
import By
from selenium.webdriver.support.ui
import WebDriverWait
from selenium.webdriver.support
import expected_conditions as EC
from selenium.webdriver.common.keys
import Keys
from selenium.common.exceptions
import NoSuchElementException
import time

PATH = r "C:\Users\GUTKRISH\chromedriver.exe"

driver = webdriver.Chrome(PATH)

driver.get("http://demo.guru99.com/test/newtours/")
try:
user_name = driver.find_element_by_name("userNam")
user_name.send_keys("mercury")
except NoSuchElementException:
   print("exception handled")
password = driver.find_element_by_name("password")
submit = driver.find_element_by_name("submit")

password.send_keys("mercury")
submit.click()