I want to fetch text from a div, but there are allot of duplicated classes. The only way to filter my search is by checking for a specific text within a sibling. Right now this is what I got:
accountmanager = ()
def send_keys_in_loop_dropaccountmanager(locator):
for i in range(5):
try:
global accountmanager
test = wait.until(EC.element_to_be_clickable(locator)).text
print(test)
accountmanager = test
break
except:
pass
send_keys_in_loop_dropaccountmanager((By.XPATH, "//div[contains(@class,'ahoy-value')] and following-sibling::div[contains(text(),'Accountmanager')]"))
print("accountmanager:", accountmanager)
I get no response at all.
Google inspector code(text that I want selected in blue):```
CodePudding user response:
You can locate the parent element with class ahoy-label-value-pair
based on known child element text content and then find the another child of that parent, as following:
"//div[@class='ahoy-label-value-pair'][contains(.,'Accountmanager')]//div[@class='ahoy-value']"
The selenium code for this will look as following:
accountmanager = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='ahoy-label-value-pair'][contains(.,'Accountmanager')]//div[@class='ahoy-value']"))).text
print("accountmanager name: ", accountmanager)
CodePudding user response:
I figured it out, by removing the loop, checking other posts, and dubble checking, my xpath, I came up with the following:
element = wait.until(EC.visibility_of_element_located((By.XPATH,"//div[contains(text(), 'Accountmanager')]/following-sibling::div")))
accountmanager = element.text
print("accountmanager:", accountmanager)
CodePudding user response:
Try the following xpath -
//div[text()='Peter Hendrik'][@class='ahoy-value']
Edit: If you want to go through the Accountmanager
text, you can use the following xpath -
//div[text()='Accountmanager'][@class='ahoy-label']/following-sibling::div[@class='ahoy-value']