How to solve this InvalidSelectorException in pytest? - python

When I try to scroll until the presence of particular text by using a scroll utility its toasting InvalidSelectorException. I need to scroll to a text in mobile screen I am using pytest Appium framework
class ScrollUtil:
#staticmethod
def scroll_to_text_by_android_uiautomator(text, driver):
driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,(
"new UiScrollable(new UiSelector().scrollable(true).instance(" "0)).scrollIntoView(new UiSelector().textContains(\"" + text + "\").instance(0)")).click()
def follow_and_unfollow_button_working_people_you_may_know(self):
ScrollUtil.scroll_to_text_by_android_uiautomator("People you may know",self.driver)
self.click("follow_button_text")
follow_working = self.display_result("unfollow_button_text")
while follow_working == True:
print("Working of follow button is verified in People you may know")
break
self.click("unfollow_button_text")
unfollow_working = self.display_result("follow_button_text")
while unfollow_working == True:
print("Working of unfollow button is working in People you may know")
break
Here the code which i am trying

Related

I have a code that automates cookieclicker, it also clicks all the upgrades automatic. the only thing I still want to add is to click the golden cooki

Does anyone know how I can add this in my code? This is the code:
def cookieclicker(num):
cookie = driver.find_element_by_id("bigCookie")
for i in range(num):
cookie.click()
def store_upgrades():
products = driver.find_elements_by_class_name("upgrade")
for upgrade in products:
upgrade.click()
def upgrades():
items = driver.find_elements_by_class_name("enabled")
for item in items[::-1]:
item.click()
#main loop
def main():
while True:
cookieclicker(80)
try:
upgrades()
except:
continue
try:
store()
except:
continue
if keyboard.is_pressed("q"):
break
main()
As you can see the code consists of 4 function which click the cookie and buys all the upgrades. It would be great if anyone could help with the last step, the golden cookie!
I run game and observed HTML and it seems it shows golden cookie in
<div id="shimmers">
and it adds
<div class="shimmer" alt="Golden cookie" ...>
so you could search it using class shimmer
try:
find_element_by_class_name('shimmer').click()
except:
pass
or using xpath //div[#alt="Golden cookie"] if you selected English language.
In other languages it uses native name - i.e in Polish it uses alt="Złote ciastko".
try:
find_element_by_xpath('//div[#alt="Golden cookie"]').click()
except:
pass
But I didn't test if it work.
HTML in DevTool:
Full browser:

Handling pyTelegramBotAPI floating keyboard value

I'm new on making Telegram chatbot using pyTelegramBotAPI. In this case, i have multiple choices, the 'Trapezoid' and 'Simpson'. Here is the following code
#bot.message_handler(commands=['calculate'])
def welcome(message):
print(message)
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
markup.add('Trapezoid', 'Simpson')
reply = bot.reply_to(message, "Select computation method" ,reply_markup=markup)
if(reply.text == 'Trapezoid'):
bot.register_next_step_handler(reply, trapezoid_handler)
elif(reply.text == 'Simpson'):
bot.register_next_step_handler(reply, simpson_handler)
def trapezoid_handler(message):
bot.send_message(message.id, "Trapezoid Block")
def simpson_handler(message):
bot.send_message(message.id, "Simpson Block")
Here is a picture when i run the /calculate command
Here is picture when i press the 'Trapezoid' button
As you can see, when i press the 'Trapezoid' button, the trapezoid_handler was not executed.
The goal is, when i press either 'Trapezoid' or 'Simpson' button, it later move to the following button value. Am i access the floating keyboard value correctly? How do i access the floating keyboard value?
Thank you for your respond
Here, it returns reply text (i.e "Select computation method") So you're not getting actual value when you click buttons.
Fixed Code :
#bot.message_handler(commands=['caluculate'])
def welcome(message):
print(message)
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
markup.add('Trapezoid', 'Simpson')
reply = bot.reply_to(message, "Select computation method" ,reply_markup=markup)
bot.register_next_step_handler(reply, markup_handler)
def markup_handler(message):
if(message.text == 'Trapezoid'):
bot.send_message(message.chat.id, "Trapezoid Block")
elif(message.text == 'Simpson'):
bot.send_message(message.chat.id, "Simpson Block")

Button not clicking and getting no errors with Selenium Python

this buttons are not getting clicked
agree_button = driver.find_element_by_xpath('//*[#id="rightsDeclaration"]')
submit = driver.find_element_by_xpath('//*[#id="submit-work"]')
def upload():
agree_button.click()
time.sleep(1)
submit.click()
upload():
however i noticed that when the function is called, the url gets selected like this:
Url selected when the function is called
i tried locating with the id, the name , the xpath , everything and nothing
the buttons:
and also i notcied that the page scrolls a bit down (and not clicking ofc) and that's it. what's the problem?
full code:
copy_settings_link = "https://www.redbubble.com/portfolio/images/68171273-only-my-dog-understands-me-beagle-dog-owner-gift/duplicate"
def copy_settings():
driver.get(copy_settings_link)
replace_image_button = driver.find_element_by_xpath('//*[#id="select-image-base"]')
time.sleep(1)
replace_image_button.send_keys(Path_list[0])
upload_check = driver.find_element_by_xpath(
'//*[#id="add-new-work"]/div/div[1]/div[1]/div[1]/div') # CHECKING UPLOAD
while True:
if upload_check.is_displayed() == True:
print('Uploading...')
time.sleep(1)
else:
print('Uploading Finished')
time.sleep(1)
break
copy_settings()
def upload():
agree_button.click()
time.sleep(1)
submit.click()
upload()
Problem Fixed Guys:
this
chrome_options.add_argument("user-data-dir=C:\\Users\\Username\\AppData\\Local\\Google\\Chrome\\User Data") # Stay LOGGED IN
can anyone explain please to me how this code works? it let me stay logged in in the site

Trying to ensure that correct window is active right now

I'm trying to assert that the currently needed browser window is opened via selenium.
My approach is to compare titles of the windows to each other, and if title doesn't match - switch to the next window and repeat procedure. But right now check of the last window (which is correct) doesn't happen.
Method for collecting all opened browser windows:
def collect_windows(self):
windows = []
try:
for handle in self.driver.window_handles:
windows.append(handle)
return windows
except:
self.log.error(format_exc())
Method that runs through the list and checks titles of the windows:
def switch_window(self, window_title=''):
windows_list = self.collect_windows()
try:
for window in windows_list:
title = self.driver.title
if window_title not in title:
self.driver.switch_to.window(window)
self.log.info(f"Switched to window: {window_title}")
except:
self.log.error(format_exc())
Instead of comparing titles compare the window handles
def get_current_window_handle(self):
return self.driver.current_window_handle
def switch_window(self, current_handle):
for handle in self.driver.window_handles:
if handle != current_handle:
self.driver.switch_to.window(handle)
self.log.info(f"Switched to window: {self.driver.title}")
return
current_window_handle = get_current_window_handle()
# open new window
switch_window(current_window_handle)

Pywinauto automation fails when there is no user session

I am working on program automation (program named SEO indexer). I wrote the automation using python's library name pywinauto.
Everything works just great when I am running the automation over RDP connection to the server. But when I am trying to leave the program and disconnect from RDP the "Save AS" windows window is not starting and the program crashes ...
someone knows how can I fix it?
the code that is responsible to save the file is -
def run(self, process_id, link):
controls = self._app[u'TForm1']
text_box = controls.Edit
text_box.set_text(link)
button = controls.ToolBar1
windows = Desktop(backend="uia")
button.click()
self.wait_for_finish(process_id)
result_box = controls.TVirtualStringTree
result_box.RightClick()
sleep(1)
windows_list = windows.windows()
context_menu = windows.window(best_match = "Context")
save_all_button = context_menu.children()[2]
save_all_button.select()
save_as = windows.window(best_match = "save_as")
properties_section = save_as.children()[0]
file_name = "C:\\Windows\\Temp\\indexer_" + str(randint(0, 10000))
file_name_label = properties_section.children()[4].children()[0]
file_name_label.set_text(file_name)
save_button = save_as.children()[2]
save_button.click()
sleep(2)
yes_no_dialog = windows.window(best_match = "GSA SEO Indexer v2.34")
yes_no_dialog.children()[0].click()
return file_name
it crashed on -
save_as = windows.window(best_match = "save_as")
there is a way to force it opening the save as dialog even if there are no screen ?
UPDATE:
I Just notices that the problem is not that the Save as panel is not created, the problem is that when I am without screen and trying to select from the context menu (which is created) - just the text is selected, without clicking on it
The Remote Execution Guide is what you need. This is common problem for any GUI automation tool. So the question is not exactly pywinauto related, but I wrote this guide a year ago to address it for many users.

Categories