I would like to find text in found element by selenium. Look at this. For example there I would like click in 'secure transaction' at product card
I try to do it by this
class ProductCard:
def __init__(self, driver):
self.driver = driver
div = (By.XPATH, "//div[@class='a-box-inner a-padding-base']")
def easyToAssemble(self):
self.driver.find_element(*ProductCard.div).find_element(By.XPATH, "//span[contains(text(),'Secure transaction')]").click()
return True
This code doesn't work, I suspect that problem is there
self.driver.find_element(*ProductCard.div).find_element(By.XPATH, "//span[contains(text(),'Secure transaction')]")
I have no idea how to solve the issue.
I want to know how to find element in found element by selenium in python
CodePudding user response:
We can locate elements in span class and not unique id with the help of the Selenium webdriver. We can identify an element having a class attribute with the help of the locator xpath, css or class name.
To locate elements with these locators we have to use the By.xpath, By.xpath or By.cssSelector method. Then pass the locator value as a parameter to this method.
Let us see the html code of a button having a span class and try to identify it.
from selenium import webdriver
#set chromodriver.exe path
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
driver.implicitly_wait(0.5)
#launch URL
driver.get("https://www.stackoverflow.com/index.htm")
l = driver.find_element_by_id("textemail")
l.send_keys("[email protected]")
#get value entered
s = l.get_attribute('value')
#identify element with span class
m = driver.find_element_by_xpath("//span[@class='input_group_button']")
#verify if element present
b = m.is_displayed()
if b:
print("Element with span class available")
else:
print("Element with span class not available")
#close browser
driver.close()
CodePudding user response:
Error : Element <span>...</span> is not clickable at point (855, 565).
OK! So, let's click on its parent.
You just need to add /..
at the end of your xpath.
self.driver.find_element(*ProductCard.div).find_element(By.XPATH, "//span[contains(text(),'Secure transaction')]/..").click()