Selenium python click button w\ partial ID - python-3.x

I wrote some python code that logs in automatically on outlook, and writes emails and send them. The problem is that the button 'Write new email' is identified on the script online as _ariaId_ followed by any random number between 1 and 999. Therefore on that part of my script I have
nextButton = browser.find_element_by_xpath('//*[contains(#id, "_ariaId_")]')
nextButton.click()
But for some reason it tells me that it cannot find it.

The following code may work for you.
newEmailButton = browser.find_element_by_xpath("//button[contains(.,'New mail')]")
newEmailButton.click();
sendButton = browser.find_element_by_xpath("//button[contains(.,'SEND')]")
sendButton.click();

Use xpath :
//span[normalize-space()='Nuovo']

<span class="label o365buttonLabel _fce_r _fce_n" id="_ariaId_26">Nuovo</span>
That's what I need to identify, that ariaId which changes everytime.

Related

Checking status/event of icon - Selenium, Python 3.9

With covid and online schooling, it's hard to keep up with my kid keeping up! I'm only a little familiar with Python and less familiar with webstuff and selenium, but I wanted to try to make it easier to check on whether he's finishing his assignments each day by writing a script that (1) goes to the class webpages, (2) looks for the 'Overdue' text in the outer HTML, and (3) does something (e.g., print 'There is an overdue assignment') if the find method succeeds.
I've completed 1 successfully and know how to do part 3, but can't figure out part 2.
I found what I think is the relevant part on the pages with the inspect element:
<i class="icon-minimize" aria-label="This assignment is overdue" title="This assignment is overdue"></i>
And I've tried the following code variations:
overDue = driver.find_element_by_tag_name("This assignment is overdue")
overDue = driver.find_element_by_name("This assignment is overdue")
And I've tried to copy the CSS Selector and use
overDue = driver.find_element_by_class_name("i.icon-minimize:nth-child(1)")
I also tried XPath, but I forget now exactly what my code was. Something like:
overDue = driver.find_element_by_xpath(//*[., text()="This assignment is overdue"])
But all of these return a NoSuchElement exception. Is there something wrong with my syntax or am I using the wrong methods?
Thanks.
overDue = driver.find_element_by_xpath('//*[#title="This assignment is overdue"]')
its title , use #title to validate the value of title attribute
xpath syntax is
//tagname[#attribute="attributevalue"]
so tag_name is i
driver.find_element_by_tag_name("i")
there is no name attribute so you cannot use
driver.find_element_by_name
class is 'icon-minimize' you should not mention tag there:
driver.find_element_by_class_name("icon-minimize")

Selenium Select Input Box that Changes Name Every Session

I'm trying to access a search text box inside of our company's ERP system using Selenium. The screenshot shows the text box and the Xpath of the element.
This is a little tricky, because that Menu Search pop-up isn't really a pop-up. It somehow shows up when a user types Control + M.
By installing ChroPath and testing I've found the text-box always starts with the following string:
txtMenuSearch_Namespace_
I've tried to imitate what's described here, here and here with no luck.
The latest attempt in the snippet of my code looks like this:
menu_search_input_box_elements = driver.find_elements_by_xpath("//*[contains(#id, ‘txtMenuSearch_Namespace_’)]")
for item in menu_search_input_box_elements:
print(item)
I get the following error message:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[contains(#id, ‘txtMenuSearch_Namespace_’)]' is not a valid XPath expression.
In all my attempts to get the syntax right I keep getting this message. Any help in figuring out how to .send_keys() to this field is greatly appreciated.
You get en error because of the ‘’ quotes, replace them with correct ones.
# id starts with txtMenuSearch_Namespace_
menu_search_input_box_elements = driver.find_elements_css_selector("[id^='txtMenuSearch_Namespace_']")
menu_search_input_box_elements = driver.find_elements_by_xpath("//*[contains(#id, 'txtMenuSearch_Namespace_')]")

Selenium - find element by link text

I am using selenium webdriver on Chrome; python 3 on Windows 10.
I want to scrape some reports from a database. I search with a company ID and a year, the results are a list of links formatted in a specific way: something like year_companyID_seeminglyRandomDateAndDoctype.extension, e.g. 2018_2330_20020713F04.pdf. I want to get all pdfs of a certain doctype. I can grab all links for a certain doctype using webdriver.find_elements_by_partial_link_text('F04') or all of that extension with '.pdf' instead of 'F04', but I cannot figure out a way to check for both at once. First I tried something like
links = webdriver.find_elements_by_partial_link_text('F04')
for l in links:
if l.find('.pdf') == -1:
continue
else:
#do some stuff
But unfortunately, the links are WebElements:
print(links[0])
>> <selenium.webdriver.remote.webelement.WebElement (session="78494f3527260607202e68f6d93668fe", element="0.8703868381417961-1")>
print(links[0].get_attribute('href'))
>> javascript:readfile2("F","2330","2015_2330_20160607F04.pdf")
so the conditional in the for loop above fails.
I see that I could probably access the necessary information in whatever that object is, but I would prefer to do the checks first when getting the links. Is there any way to check multiple conditions in the webdriver.find_elements_by_* methods?
You can try to use below code
links = [link.get_attribute('href') for link in webdriver.find_elements_by_partial_link_text('F04') if link.get_attribute('href').endswith('.pdf")')]
You can also try XPath as below
links = webdriver.find_elements_by_xpath('//a[contains(., "F04") and contains(#href, ".pdf")]')
Andersson's approach seems to work with a slight correction:
if link.get_attribute('href').endswith('.pdf')] rather than if link.get_attribute('href').endswith('.pdf")')], i.e. delete the ").

I cannot send keystrokes to a selected input box

from pywinauto.application import Application
app = Application().Start(cmd_line=u'"path to program" ')
afx = app[u'Afx:01360000:0']
afx.Wait('ready')
afxtoolbar = afx[u'1']
toolbar_button = afxtoolbar.Button(3)
toolbar_button.Click()
window = app.Dialog
window.Wait('ready')
edit = window.Edit4
edit.Click()
app.typekeys ("Success")
So at this point, I've gotten the application to open, the correct window to pop up and also a mouse click on the box that I want to populate with a short string. I cannot for the life of me, figure out how to pass keyboard input to this field. I have read all the docs for PyWinAuto, and nothing is helping...
Basically all I need to do is figure out how to send a string, and then how to send the TAB key six times. I can then finish my program to automate this application.
I am also using Swapy64bit to help. The program uses a win32 backend. I'm using Python 3.6.
Am I not prefixing typekeys correctly? The PyWinAuto documentation leaves much to be desired.
First the correct name of the method is type_keys, but assume you use it correctly.
The reason might be losing focus at the edit control because type_keys tries to set focus automatically. The solution is:
app.type_keys("Success{TAB 6}", set_foreground=True)

How to send "Ctrl + c" in Sikuli?

This feels like it should be pretty easy but I can't find documentation on how to do this:
I just want Sikuli to type Ctrl+C to copy text to the clipboard.
type(KEY_CTRL+'c') doesn't work and neither does type(KEY_CTRL,'c').
Any suggestions?
Try using type("c",KEY_CTRL) instead.
I wrote a simple script which types a line in notepad, double clicks it to mark it and then ctrl+x ctrl+v it into the document again. Works great.
openApp("notepad.exe")
find("textfield.png" )
type("Some text")
doubleClick("theText.png")
type("x", KEY_CTRL)
click("theTextField.png" )
type("v",KEY_CTRL)
The following works in 0.9 and newer versions of sikuli
type('x', KeyModifier.CTRL)
Key objects are defined for pretty much all the modifier keys and num pad keys. Anyways, it should look something like this
keyDown(Key.CTRL)
type('c')
keyUp(Key.CTRL)
The usage of type() and the possible key names are documented here:
http://doc.sikuli.org/region.html#Region.type
http://doc.sikuli.org/keys.html#key-constants
As others have mentioned, use the following:
type('c', Key.CTRL) # Copy command
One point worth mentioning - do not use upper-case characters, i.e.:
type('C', Key.CTRL) # Does not copy, avoid this
I haven't looked into the Sikuli source code, but my best guess is that it implicitly sends this as Shift+C, which results in a different command entirely.
type('x', Key.CTRL) also works.
Also, make sure that NUM_LOCK is off. If NUM_LOCK is on, it can make anything with KeyModifier.CTRL or KeyModifier.SHIFT misbehave.
You can try next code:
keyDown(Key.CTRL)
type("c")
keyUp(Key.CTRL)
I had a requirement to automate a flash content. The following code worked for me.
These were the following steps I ahd to perform as a part of the automation:
Enter Username and Password
Click on Login Button
Click on the button which will navigate to the application
The challenge I faced was to focus on the Username and password which had no placeholders . Hence the focusing was difficult. So I used the CTRL keys to do this .
Pattern appLogo = new Pattern("C:\\images\\appLogo.png");
StringSelection userNameText = new StringSelection("username");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(userNameText, null);//Copy the text into the memory
Screen s = new Screen();
s.find(appLogo);
s.click(appLogo);
s.type(Key.TAB);//I had to enter tab twice to focus on user name textbox
s.type(Key.TAB);
s.type("V",KeyModifier.CTRL);
StringSelection password = new StringSelection("password");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(password, null);
s.type(Key.TAB);//I had to enter tab twice to focus on user name textbox
s.type("V",KeyModifier.CTRL);
Pattern loginButton = new Pattern("C:\\images\\Login.png");
s.find(loginButton);
s.doubleClick(loginButton);
The scenario is like i need to press say key E in my keyboard after finishing the test how to add this in the script in Sikuli IDE.

Resources