0

Below is the inspect of the element

<div class="input-group ref-container ">
<input id="sys_display.incident.assignment_group" name="sys_display.incident.assignment_group" aria-labelledby="label.incident.assignment_group" type="search" autocomplete="off" autocorrect="off" value="PeopleSoft Reporting ONLY" ac_columns="u_full_name" data-type="ac_reference_input" data-completer="AJAXTableCompleter" data-dependent="" data-dependent-value="" data-ref-qual="" data-ref="incident.assignment_group" data-ref-key="null" data-ref-dynamic="false" data-name="assignment_group" data-table="sys_user_group" class="form-control element_reference_input  " style="; " spellcheck="false" onfocus="if (!this.ac) addLoadEvent(function() {var e = gel('sys_display.incident.assignment_group'); if (!e.ac) new AJAXTableCompleter(gel('sys_display.incident.assignment_group'), 'incident.assignment_group', '', ''); e.ac.onFocus();})" aria-required="true" role="combobox" aria-autocomplete="list" aria-owns="AC.incident.assignment_group">
<span class="ref_dynamic_placeholder">A new record with this value will be created automatically</span>
<span class="input-group-btn">
<button id="lookup.incident.assignment_group" name="lookup.incident.assignment_group" type="button" class="btn btn-default" title="Lookup using list" aria-haspopup="true" data-for="sys_display.incident.assignment_group" data-type="ac_reference_input" tabindex="-1" role="button" aria-label="Look up value for field: Assignment group" data-original-title="Lookup using list">
<span class="icon icon-search" aria-hidden="true">
</span>
</button>
</span>
</div>

How can I write the value "PeopleSoft Reporting ONLY" into a variable?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83

2 Answers2

2

You can select by id= attribute. For example:

txt = '''<div class="input-group ref-container "><input id="sys_display.incident.assignment_group" name="sys_display.incident.assignment_group" aria-labelledby="label.incident.assignment_group" type="search" autocomplete="off" autocorrect="off" value="PeopleSoft Reporting ONLY" ac_columns="u_full_name" data-type="ac_reference_input" data-completer="AJAXTableCompleter" data-dependent="" data-dependent-value="" data-ref-qual="" data-ref="incident.assignment_group" data-ref-key="null" data-ref-dynamic="false" data-name="assignment_group" data-table="sys_user_group" class="form-control element_reference_input  " style="; " spellcheck="false" onfocus="if (!this.ac) addLoadEvent(function() {var e = gel('sys_display.incident.assignment_group'); if (!e.ac) new AJAXTableCompleter(gel('sys_display.incident.assignment_group'), 'incident.assignment_group', '', ''); e.ac.onFocus();})" aria-required="true" role="combobox" aria-autocomplete="list" aria-owns="AC.incident.assignment_group"><span class="ref_dynamic_placeholder">A new record with this value will be created automatically</span><span class="input-group-btn"><button id="lookup.incident.assignment_group" name="lookup.incident.assignment_group" type="button" class="btn btn-default" title="Lookup using list" aria-haspopup="true" data-for="sys_display.incident.assignment_group" data-type="ac_reference_input" tabindex="-1" role="button" aria-label="Look up value for field: Assignment group" data-original-title="Lookup using list"><span class="icon icon-search" aria-hidden="true"></span></button></span></div>'''

soup = BeautifulSoup(txt, 'html.parser')

s = soup.select_one('#sys_display\.incident\.assignment_group')['value']
print(s)

Prints:

PeopleSoft Reporting ONLY

The same with:

s = soup.find(id="sys_display.incident.assignment_group")['value']
print(s)
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0

Using Selenium to extract the text PeopleSoft Reporting ONLY you can use either of the following solutions:

  • Using css_selector:

    print(driver.find_element_by_css_selector("input[id^='sys_display'][name*='incident'][aria-labelledby$='assignment_group']").get_attribute("value"))
    
  • Using xpath:

    print(driver.find_element_by_xpath("//input[starts-with(@id, 'sys_display')][contains(@name, 'incident')][contains(@aria-labelledby, 'assignment_group')]").get_attribute("value"))
    

However as per best practices to extract/print the desired text you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".data-row"))).get_attribute("value"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[@class='data-row']"))).get_attribute("value"))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

Update

As using:

  • find_element_by_* you face NoSuchElementException
  • WebDriverWait you face TimeoutException

Possibly the element is not within the Top Level Content and may be within an <iframe>. Now to address NoSuchElementException follow this and this discussion.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Getting the below the error : selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: – dibyajyoti sahoo May 26 '20 at 11:25
  • Try the second part with _WebDriverWait_ – undetected Selenium May 26 '20 at 11:27
  • Now i get this error : raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: – dibyajyoti sahoo May 26 '20 at 11:41
  • So that implies the element is not within the _Top Level Content_ and possibly within a `iframe`. Now to address _NoSuchElementException_ follow [this](https://stackoverflow.com/questions/47993443/selenium-selenium-common-exceptions-nosuchelementexception-when-using-chrome/47995294#47995294) and [this](https://stackoverflow.com/questions/50315587/selenium-common-exceptions-nosuchelementexception-message-no-such-element-una/50315715#50315715) discussion. – undetected Selenium May 26 '20 at 11:46