Calvin  Hobbes
Calvin Hobbes

Reputation: 87

Python + Selenium: help finding alternative find_element_by

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)

Upvotes: 0

Views: 842

Answers (2)

Sers
Sers

Reputation: 12255

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

Upvotes: 1

Ratmir Asanov
Ratmir Asanov

Reputation: 6459

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!

Upvotes: 1

Related Questions