0

I need a bit of help finding an alternative way to call for this element. I was using the find_element_by_id, under the assumption that the last 7 numbers were my students ORGID assign numbers assigned from D2L, turns out they are not. I don't know how that number is generated.

If there another way I can call this item? Maybe using the title?

<a class="d2l-imagelink" id="ICN_Feedback_3400653_125401" 
href="javascript:void(0);" onclick="return false;" title="Enter comments for 
FIRSTNAME LASTNAME in a new window" aria-label="Enter comments for FIRSTNAME 
LASTNAME in a new window" role="button"><d2l-icon icon="d2l-tier1:edit" 
class="x-scope d2l-     icon-0">

Sorry, I cannot post a link to the page, it is password protected and legally cannot give out student names. originally had it as find_element_by_id(ICN_Feedback_3400653_125630). the reason I can't use that is that last set of numbers (125630) seem to be an ID code for individuals, but I don't have access to the list of what those codes are.

I am looking to click on that little pencil on the far right, with names taken out the code for it is:

<a class="d2l-imagelink" id="ICN_Feedback_3444653_124440" 
 href="javascript:void(0);" onclick="return false;" title="Edit comments for 
 FIRSTNAME LASTNAME in a new window" aria-label="Edit comments for FIRSTNAME 
LASTNAME in a new window" role="button">

My code:

driver = webdriver.Chrome(chrome_path) 
driver.get(commentsPage)
assert "****" in driver.title

user = driver.find_element_by_name("userName")
user.clear()
user.send_keys("USERNAME")

pas = driver.find_element_by_name("password")
pas.clear()
pas.send_keys("PASSWORD")
user.send_keys(Keys.RETURN)

driver.get(commentsPage)

for i in toplist:

    icnFeedback = (""" "//a[@title='Enter comments for """+ i[0] + """ in a 
    new window']"  """)
    myElement = driver.find_element_by_xpath(icnFeedback)                       
    # find user by orgid
    driver.execute_script("arguments[0].click();", myElement)                 
    #clicks the feedback button

    time.sleep(2)
    iframes2 = driver.find_elements_by_tag_name("iframe")                     
    #looks for the iframes on main page
    driver.switch_to.frame(iframes2[1])                                       
    #this switches from main page to the iframe#2
    time.sleep(1)
    iframes3 = driver.find_elements_by_tag_name("iframe")                     
    #looks for the iframes inside iframe#2
    driver.switch_to.frame(iframes3[0])                                       
    #this switches from iframes#2 to iframe#3
    time.sleep(1)
    textBox = driver.find_element_by_id('tinymce')                            
    #finds textbox 

    comments = i[1] 
    textBox.clear()                                                           
    #clears previous text
    textBox.send_keys(comments)                                               
    #send comments
    time.sleep(2)
    driver.switch_to.default_content()                                        
    #switches out of all iframes
    iframes2 = driver.find_elements_by_tag_name("iframe")                     
    #looks for the iframes on main page
    driver.switch_to.frame(iframes2[1])                                       
    #this switches from main page to the iframe#2
    button = driver.find_element(By.XPATH, '//button[text()="Save"]').click() 
    #looks for save button
    time.sleep(1)
1
  • I'm assuming this is a link in some table of data? Please post a link to the page or at least the row of relevant data. My guess is that a locator can be created where you can pass in some data like a student name, ID, etc. and then get the link for that row of data. Commented Aug 20, 2018 at 0:34

2 Answers 2

1

By xpath and title:

driver.get_element_by_xpath("//a[@title='Enter comments for FIRSTNAME LASTNAME in a new window']")

To remove any gaps in text:

driver.get_element_by_xpath("//a[normalize-space(@title)='Enter comments for FIRSTNAME LASTNAME in a new window']")

driver.get_element_by_xpath("//a[contains(@title,'FIRSTNAME LASTNAME') and @class='d2l-imagelink']")

driver.get_element_by_xpath("//a[contains(@title,'FIRSTNAME LASTNAME') and @role='button']")

driver.get_element_by_xpath("//a[contains(@title,'FIRSTNAME LASTNAME') and @class='d2l-imagelink' and @role='button']")

driver.get_element_by_xpath("//a[contains(@title,'FIRSTNAME') and contains(@title,'LASTNAME') and @class='d2l-imagelink'")

and so on

Sign up to request clarification or add additional context in comments.

2 Comments

it worked the first time, now it is having problems find the xpath
driver.get_element_by_xpath("//a[contains(@title,'FIRSTNAME LASTNAME') and @class='d2l-imagelink']") worked wonders. thank you
1

You can use the following:

link = driver.find_elements_by_css_selector(".d2l-imagelink[role='button']")

Or better to add explicit wait:

link = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".d2l-imagelink[role='button']")))

Hope it helps you!

3 Comments

Thanks, how do i isolate for different links on the page, there can be up to 200 of those.
In this case, you can use driver.find_elements_by_link_text("your link text").
@CalvinHobbes, let me know about the result. Thanks.

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.