I need to move an user with selenium to a list, I tried with drag and drop command but I cant do it, the only way that the platform lets me move a user to the list is by drag and drop it to list so
any help?
import email
from importlib.resources import path
from lib2to3.pgen2 import driver
import webbrowser
import unittest
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
path="C:\\Users\\ysarmiento\\Desktop\\PythonVsCode\\chromedriver.exe"
username="obviusly not gonna show"
password="same"
path="C:\\Users\\ysarmiento\\Desktop\\PythonVsCode\\chromedriver.exe"
driver = webdriver.Chrome(path)
driver.get("https://beanalyzer.teldat.com/login")
driver.maximize_window()
driver.find_element_by_id("email").send_keys(username)
driver.find_element_by_id("password").send_keys(password)
driver.find_element_by_xpath("/html/body/div/div[2]/div[1]/form/button").click()
sleep(3)
driver.get("https://beanalyzer.teldat.com/settings/system-provisioning")
target=driver.find_element_by_xpath('//*[#id="main-el"]/div[3]/div/div[2]')
value=driver.find_element_by_xpath('//*[#id="peculiar"]/div/div/div[1]')
actions=ActionChains(driver)
actions.drag_and_drop(target,value).perform()
sleep(4)
I have this and it doesn't give me any errors but it doesn't work, for locating the elements I'm using Xpath (cause some users and lists doesn't have an id or name)
I'm putting here the type of list and user(element) if needed
User Element
type of list
if you need something else let me know
thank you for your time
Related
I have been working on this problem for quite a bit now, and can't figure out why this is happening. I am trying to click a button. The button and the corresponding text always changes, but the XPATH stays the same.
Button I am trying to click
It doesn't work with CSS Selector either. I am using Chrome web version 106. Does anyone know why?
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
PATH = r"C:\Users\###\Downloads\chromedriver.exe"
driver = webdriver.Chrome(PATH)
# time to login
driver.get("https://clever.com")
time.sleep(60)
try:
driver.find_element(By.XPATH, '//*[#id="root"]/div/div/div[2]/div[3]/div/div/div/div[2]/button[2]').click()
except NoSuchElementException:
print("no such element")
pass
time.sleep(5)
Capturing the XPATH for the element
Try this:
driver.find_element("xpath", '//*[#id="root"]/div/div/div[2]/div[3]/div/div/div/div[2]/button[2]').click()
May be you using not correct syntax
Is it possible to make a program in python in which the program will look for a message on WhatsApp sent by a particular person on a group(which I have pinned for convenience) at a particular time and then it will print the message sent by that person?
Well if you don't understand my question let me give you an example:
Suppose there is a group named ABCD which is pinned on WhatsApp web(which means it is at the top)
It consists of 4 people - A,B,C,D
I want the program to print the message sent by C at time 13:05 in that group
Is this thing possible on python?
I can use any module like selenium or even pyautogui
Yes, it is possible through Selenium without using any kind of API.
First we will import required modules:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
Now we will launch Whatsapp Web:
driver = webdriver.Chrome()
driver.get("https://web.whatsapp.com")
You can use other browsers, for this example we will be using Chrome.
Now we will locate the Search or Start New Chat button using XPath and add a webdriver wait for it:
search_button = WebDriverWait(driver,50).until(lambda driver: driver.find_element_by_xpath("//input[#title='Search or start new chat']"))
Now we will click it and add a little sleep:
search_button.click()
time.sleep(2)
Now we will Send the name of the Person to contact into the text area:
search_button.send_keys("A")
time.sleep(2)
Now we will send a message
input_box = driver.find_element_by_xpath(r'//div[#class="_2S1VP copyable-text selectable-text"][#contenteditable="true"][#data-tab="1"]')
time.sleep(2)
input_box.send_keys("Hello" + Keys.ENTER)
time.sleep(2)
You can using Chrome inspect more and do more things.
New to Selenium. I'm trying to get it so that I can enter a name into a search box and then click on the correct name.
I have managed to write some code to go to a website and then enter what I want and press the search button. The problem is that it then shows a list of items so I'd have to click again.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from webdriver_manager.chrome import ChromeDriverManager
from time import sleep
index = 'AMZN'
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://www.marketscreener.com')
sleep(6)
text_box = driver.find_element_by_css_selector('#autocomplete')
text_box.send_keys(index)
#select.select_by_index(1)
driver.find_element_by_xpath("//*[#id='recherche_menu']/table/tbody/tr[1]/td/button/img").click()
When I enter the name and don't click It presents a dropdown list so I am trying to select the first item from the dropdown as that is usually what I will want.
I attempted this by using the commented out select.select_by_index code line. But it doesn't quite work.
I just tried also using text_box.send_keys(Keys.DOWN, Keys.RETURN) to move down into the dropdown field, but this doesn't work and just returns the same as what I currently get from clicking.
To be clear what I mean is that currently the code will return this:
But I want it to go straight to the Amazon page so it will return this:
Any help appreciated.
Thanks
This page use many JavaScript to listen the action of the mouse and the key.If you didn't focus on the searchbox,the dropdown will be disappeared.(and you couldn't find it in the source code).
Try to use ActionChains,this works fine on my PC:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
index = 'AMZN'
driver = webdriver.Chrome()
driver.get('https://www.marketscreener.com')
action = ActionChains(driver)
sleep(4)
text_box = driver.find_element_by_css_selector('#autocomplete')
action.move_to_element(text_box)
action.click(text_box)
action.send_keys(index)
action.perform()
sleep(1)
driver.find_element_by_xpath('//*[#id="AC_tRes"]/li[1]').click()
If the result couldn't show you,maybe you need to increase the time of sleep().
I'm new in Selenium with Python and I have a problem with ActionChains , that I couldn't understand.I want to click on an element and move it to another element with ActionChain , I tried 2 ways to do this .
Firstly the combination of 2 py-files , they don't work
import time
from selenium.webdriver.common.action_chains import ActionChains
def action_function(driver,start,des):
time.sleep(2)
ActionChains(driver).click_and_hold(start).move_to_element(des).release().perform()
time.sleep(3)
import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from hilffunktionen import hilffunktion
class PythonOrgSearch(unittest.TestCase):
driver = webdriver.Firefox('./geckodriver')
#classmethod
def firsttest(self):
self.driver.get('file:///C:/My-Project/Probe/index.html')
time.sleep(5) # Let the user actually see something!
dragitem = self.driver.find_element_by_id('mydivheader')
print(dragitem.get_attribute('innerHTML'))
time.sleep(5)
destination = self.driver.find_element_by_id('destination')
time.sleep(4)
hilffunktion.action_function(self.driver,dragitem,destination)
time.sleep(3)
But if I try to write it direct in class , it works
import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
class PythonOrgSearch(unittest.TestCase):
driver = webdriver.Firefox('./geckodriver')
driver.get('file:///C:/My-Project/Probe/index.html')
time.sleep(5) # Let the user actually see something!
dragitem = driver.find_element_by_id('mydivheader')
print(dragitem.get_attribute('innerHTML'))
time.sleep(5)
destination = driver.find_element_by_id('destination')
time.sleep(4)
ActionChains(driver).click_and_hold(dragitem).move_to_element(destination).release().perform()
time.sleep(3)
Could someone explain me why ? , If I just want to write it in the first way , what should I do , so that it works ? . I would be very thankful for you help
The second way "works" because
A class definition is an executable statement.
(see more in class-definitions)
Basically python runs the statements in class definition.
If you want to do the first way (assume that you want to use unittest), maybe you can define the firsttest method as test_xyz(self): ... and in the end you can call unittest.main(), similar in the basic example.
I am automating a boring data entry task, so I created a program that basically clicks and types for me using selenium. It runs great! except for when it reaches this specific "Edit Details..." element that I need clicked, however, selenium is unable to locate the element regardless of whatever method I try.
I've tried using a CSS selector that tried to access the ID to no avail. I also tried using XPATH, as well as trying to be more specific by giving it a 'contains' statement with the button text. As last resort, I used the selenium IDE to see what locator it registers when I physically click the button and it used the exact same ID that I state in my code. I am completely lost on how to go about fixing this.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import *
import pyautogui as py
import time, sys, os, traceback
#Launching Browser
browser = webdriver.Ie()
wait = WebDriverWait(browser, 15) #Waiting
#Laziness Functions
def clickCheck(Method, Locator, elemName):
wait.until(EC.element_to_be_clickable((Method, Locator)))
print(elemName + ' Clickable')
#Commence main function
try:
#Do alot of Clicks and Stuff until it reaches "Edit Details..." element
"""THIS IS WHERE THE PROBLEM LIES"""
time.sleep(3)
clickCheck(By.CSS_SELECTOR, 'td[id="DSCEditObjectSummary"]', "Edit Details")
elemEdit = browser.find_element_by_css_selector('td[id="DSCEditObjectSummary"]')
elemEdit.click()
#FAILSAFES
except:
print('Unknown error has Occured')
exc_info = sys.exc_info()
traceback.print_exception(*exc_info)
del exc_info
finally: #Executes at the end and closes all processes
print('Ending Program')
browser.quit()
os.system("taskkill /f /im IEDriverServer.exe")
sys.exit()
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to find element with css selector == [id="DSCEditObjectSummary"]
This is what I get as an error, all I want is for the element to be clicked just like all other elements are being located by CSS_Selectors. The image below indicates in blue the exact line for the "Edit Details..." button.
Edit Details Button
It looks like the issue may be with the page loading slowly, or as another commenter mentioned it's in an iFrame, etc. I typically try clicking by using X/Y coordinates with a macro tool like AppRobotic if you're running this on Windows. If it's an issue with the page loading slowly, I usually try stopping the page load, and interacting with the page a bit, something like this should work for you:
import win32com.client
from win32com.client import Dispatch
x = win32com.client.Dispatch("AppRobotic.API")
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import *
import pyautogui as py
import time, sys, os, traceback
#Launching Browser
browser = webdriver.Ie()
wait = WebDriverWait(browser, 15) #Waiting
#Laziness Functions
def clickCheck(Method, Locator, elemName):
wait.until(EC.element_to_be_clickable((Method, Locator)))
print(elemName + ' Clickable')
driver.get('https://www.google.com')
# wait 20 seconds
x.Wait(20000)
# scroll down a couple of times in case page javascript is waiting for user interaction
x.Type("{DOWN}")
x.Wait(2000)
x.Type("{DOWN}")
x.Wait(2000)
# forcefully stop pageload at this point
driver.execute_script("window.stop();")
# if clicking with Selenium still does not work here, use screen coordinates
x.MoveCursor(xCoord, yCoord)
x.MouseLeftClick
x.Wait(2000)
I am posting this answer more so for other folks who might be running into the same problem and stumble upon this post. As #PeterBejan mentioned, the element I was trying to click was nested in an iframe. I tried accessing the iframe except I was thrown a NoSuchFrameException. Further digging revealed that this frame was nested inside 3 other frames and I had to switch to each frame from top level down, to access the element. This was the code I used
wait.until(EC.frame_to_be_available_and_switch_to_it("TopLevelFrameName"))
wait.until(EC.frame_to_be_available_and_switch_to_it("SecondaryFrameName"))
wait.until(EC.frame_to_be_available_and_switch_to_it("TertiaryFrameName"))
wait.until(EC.frame_to_be_available_and_switch_to_it("FinalFrameName"))
clickCheck(By.ID, 'ElementID', "Edit Details")
elemEdit = browser.find_element_by_id("ElementID")
elemEdit.click()