1

I am looking for a way to extract/print text which is located within a href element.

Snapshot of the HTML: snapshot of HTML

Code trials:

WebDriverWait(driver, 20).until(EC.visibility_of_element_located(By.XPATH('//div[contains(@class, "style1")]')))
driver.find_element_by_xpath('//div[contains(@class, "style1")]').text

Error massage: TypeError: 'str' object is not callable

Many more trials to locate this "0" without success. The number will change when some open 'tickets' appear in the system.

How this could be done?

0

2 Answers 2

1

if you print this, it should return the value 0

print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH,'//div[contains(@class, "style1")]/a'))).text)

You need to change this from

driver.find_element_by_xpath('//div[contains(@class, "style1")]').text

to

print(driver.find_element(By.XPATH,'//div[contains(@class, "style1")]/a').text)

You need to import this library.

from selenium.webdriver.common.by import By
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I used your code, but still get this error: "TypeError: 'str' object is not callable"
@Paulina, Sorry I mistake. Try now. I have updated the answer.
now i get: "TypeError: __init__() takes 2 positional arguments but 3 were given"
@Paulina, Just copy exact what I have posted. if you don't want to print this just use this line WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH,'//div[contains(@class, "style1")]/a'))) I am sure you have missed one parenthesis.
0

You were close enough. The untill() method accepts a tuple. Hence you see the error:

TypeError: 'str' object is not callable

Moreover the text 0 is within the descendant <a> of the <div>.


Solution

Your effective line of code will be:

  • Using text attribute:

    print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class, 'style1')]/a"))).text)
    
  • Using get_attribute():

    print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class, 'style1')]/a"))).get_attribute('textContent'))
    

Alternative

As an alternative considering thw href attribute of the <a> tag you can use:

print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='style1']/a[starts-with(@href, '/scp/endeavour/deviationDetailsPortlet')]"))).text)

2 Comments

both lines suggested in 'Solution' shows Syntax error (pointing out s in "style1") - don't know why there. Alternative code give Timeout error.
1) both lines suggested in 'Solution' shows Syntax error (pointing out s in "style1"): my bad, corrected the bug. 2) Alternative code give Timeout error": corrected now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.