When I use MainWindow.hide() funtion, it gives me an error stating MainWindow is not defined but same MainWindow works in setupUI and retranslateUI - mysql

I am fairly new to PyQt5. So here, I am trying to work on a project in which first window in login page dialog window. So if password entered are correct it will open another window.
def login(self):
eid=self.lineEdit.text()
epass=self.lineEdit_2.text()
if eid==idd and epass==passs:
from BloodBank import Ui_MainWindow
self.MainWindow = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.MainWindow)
LoginPage.hide()
self.MainWindow.show()
else:
self.msgdlg("Wrong ID or Password!")
In this Window, from title bar menu action, I tried to attach yet another window, which when opened, current window become hidden. For this I wrote following code:
def menu(self,action):
txt=(action.text())
if txt=="Blood":
try:
from Blood import Ui_MainWindow
self.MainWindow1 = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.MainWindow1)
MainWindow.hide()
self.ui.updatelist()
self.MainWindow1.show()
except Exception as e:
print(e)
Now, When I run above code without MainWindow.hide() it run perfectly fine. But as soon as I feed MainWindow.hide() it gives following error.
name 'MainWindow' is not defined
When I try same .hide() funtion with login page, it works.
What do I need to do so I can hide my window?

Your code is a bit unclear, but i think it should be self.MainWindow.hide() because we are trying to hide our parent widget.
Edit
Try self.hide() in the bloodbank file instead of MainWindow.hide() I am assuming that in the file bloodbank you have your mainwindow.
Here is the problem, you have two files the Login File which consists of the loginpage and the bloodbank file.
when you created a the instance of self.MainWindow it is only in the namespace of the loginpage.
And you try to open a another window from this file (which is the 2nd part of your code). MainWindow instance is not present because you have defined (instantiated) in another file (login page).

Related

Selenium question: neither css selector or XPath can locate a visible and clickable web element

Here's the link to the website I'm working with: https://www.ftchinese.com/channel/stock.html
Apologies that the website is in Chinese. Basically I'm trying to get the web driver to click on the login button in the header (upper righthand side). the login button is located in the red box Then a pop up window should show up where you can enter email and password.
I tried locating the element by CSS selector: login = driver.find_element_by_css_selector('span.visitor-box > a[onclick]')
and by XPath: login = driver.find_elements(By.XPATH, '//a[#onclick]')
Both methods successfully located the login element, but when i did print(login.size,login.text), the returned height and width is 0 and text is empty. However, the login button is visible and there is no overlay. When i tried to click on the button, login.click(), it returns the error message "element not interactable".
I also tried the popular answers in similar posts - waiting time.sleep(5), maximizing/setting window sizedriver.maximize_window(), and scrolling to the element first driver.execute_script("arguments[0].scrollIntoView();", login), but had no luck. I'm not sure if it has anything to do with this particular website I'm working with, or the pop up window. Would appreciate any help!
Sharing a simple patch of code to log in to your site https://www.ftchinese.com/channel/stock.html
login_button=self.driver.find_element_by_xpath("//a[contains(text(),'登录')]")
login_button.click()
username=self.driver.find_element_by_xpath("//input[#name='username']")
username.click()
username.send_keys("{{enter username}}")
password=self.driver.find_element_by_xpath("//input[#name='password']")
password.click()
password.send_keys("{{enter password}}")
submit=self.driver.find_element_by_xpath("//input[#value='登录']")
submit.click()
Try this out and let me know if it worked for you. Cheers!
You can try with the below solution :
I am using Explicit wait, for page loading, and delay issue.
Launch the screen in full screen mode.
Do not use Chinese or English alphabets in locators.
Sample code :
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://www.ftchinese.com/channel/stock.html")
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick*='overlay-login']"))).click()
wait.until(EC.element_to_be_clickable((By.ID, "ft-login-input-username"))).send_keys("abc#gmail.com")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='password']"))).send_keys("etcasasd12#$")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class^='login-btn'] input"))).click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I Tried below xpath. And it clicked on the specified element.
driver.find_element_by_xpath("//span/a[contains(text(),'登录')]").click()

Unable to drag and drop an element under <li> to <td> using selenium Actions class methods

I'm trying to automate a use case where I should be able to drag an element which is enclosed by "ul>li" element to a target location, which is a "td" element under a table. My code is as below
WebElement source = driver.findElement(By.xpath("//div[#id='items']/ul/li[1]");
WebElement target = driver.findElement(By.xpath("//div[#id='cart']/table/tbody/tr[7]/td[3]");
Actions actions = new Actions(driver);
actions.clickAndHold(source)
.moveToElement(target)
.release(target)
.build()
.perform();
I have also tried pausing between each step, by adding
actions.moveToElement(source)
.pause(Duration.ofSeconds(2))
.clickAndHold(source)
.pause(Duration.ofSeconds(2))
.moveByOffset(1,0)
.moveToElement(target)
.moveByOffset(1, 0)
.pause(Duration.ofSeconds(2))
.release()
.pause(Duration.ofSeconds(2))
.build()
.perform();
While running in debug mode, I do see that clickAndHold is being performed, as I see the element highlighted. But the moment next action is being performed, I do not see the element dragged to the target nor released.
I'm not sure, if the issue is with the locator or the actions code.
I noticed the same behavior, if I used actions.dragAndDrop(source,target);
To simulate this, I tried to execute similar code against http://jqueryui.com/droppable/ and it is working fine. My code against this website is as below
driver.get("http://www.jqueryui.com/droppable/");
driver.manage().window().maximize();
driver.switchTo().frame(driver.findElement(By.className("demo-frame")));
WebElement drag = driver.findElement(By.xpath("//*[#id='draggable']"));
WebElement drop = driver.findElement(By.xpath("//*[#id='droppable']"));
Actions action = new Actions(driver);
//action.dragAndDrop(drag, drop).build().perform(); //This is working
action.clickAndHold(drag)
.moveToElement(drop)
.release(drop)
.build()
.perform(); // This is working
The only difference I noticed between this example and the former is, the position of the elements and how they are enclosed. In the former, the element to be dragged is under ul > li , and target location is a td element under a table.
Where as the example in jqueryui, is both are identified by id.
Also, I confirm that the xpaths used to identify the source and target in the former example are correct, as I can see them being highlighted when validated using Chrome developer tools.
Could you please suggest what can be done to fix this issue ?
Thanks
driver = new ChromeDriver();
driver.get("http://www.jqueryui.com/droppable/");
driver.manage().window().maximize();
driver.switchTo().frame(driver.findElement(By.className("demo-frame")));
//First, we capture the 1st element which we need to drag in variable "From."
WebElement From = driver.findElement(By.xpath("//*[#id='draggable']"));
//Second, we capture the 2nd element on which we need to drop the 1st element in variable "To".
WebElement To = driver.findElement(By.xpath("//*[#id='droppable']"));
//Third, we create object of Actions class as we use methods of Actions class.
Actions act=new Actions(driver);
act.dragAndDrop(From, To).build().perform();
For drag and drop element we use dragAndDrop method of Actions class and passes the parameters as the first element(Sourcelocator) "From" and the second element(Destinationlocator) "To". Below line will drag the 1st element and drop it on the 2nd element.
I faced like this issue, keep on trying different ways, this works for me:
Action dragAndDrop = builder.clickAndHold(src).moveToElement(trg).release(trg).build();
dragAndDrop.perform();
Thread.sleep(3000);
new Actions(oWebDriver).moveToElement(src).build().perform();
Thread.sleep(3000);
new Actions(oWebDriver).moveToElement(trg).click().build().perform();

Ionic3 - ElementRef nativeElement.getElementsByClassName returns collection but it is inaccessible

I'm following this tutorial about Ionic and directives and everything works fine except when I try to get the FAB element using ElementRef's nativeElement.getElementsByClassName, like this:
this.fab = this.element.nativeElement.getElementsByClassName('fab')[0]
That returns undefined. The problem is when I remove the index and print the whole HTMLCollection using console.log, it shows me a complete list with all the FAB's inside the element.
Running
console.log(this.element.nativeElement.getElementsByClassName('fab'),
this.element.nativeElement.getElementsByClassName('fab')[0]);
on ngOnInit gives the following result:
What am I doing wrong here? Every part of the code related to the problem is equal to the tutorial and it's a quite recent video...
I think the reason here is that those elements are not present while you asking for them with that line:
console.log(this.element.nativeElement.getElementsByClassName('fab'),
this.element.nativeElement.getElementsByClassName('fab')[0]);
There is simple example which shows where problem can be:
console.log(document.getElementsByClassName('fab'), document.getElementsByClassName('fab')[0]);
const el1 = document.createElement('div');
el1.setAttribute('class', 'fab');
const el2 = document.createElement('div');
el2.setAttribute('class', 'fab');
setTimeout(() => {
this.abc.nativeElement.appendChild(el1);
this.abc.nativeElement.appendChild(el2);
}, 2000);
Elements are added after 2 seconds and console log is same like yours, but when you click on HTMLCollection it will evaluate and shows you those elements - of course if you click after 2 seconds(when elements are present).
If those element are really present when you asking for them console log should look more like:
HTMLCollection(2) [div.fab, div.fab]
Also, note that this little i in Google Chrome console inform you that value is evaluted just now - at the moment when you click on it.

How to refresh particular div in the html

I am getting a problem to reflect the value in the view, I don't want to load the complete page because its very costly to load the page,
I have two controllers(controller1 and controller2), one service(service1) and two views(modalwindow.html and product.html).
The scenario is:
1.User is on product.html(contains multiple accordions) and user explicitly close all the accordions.
2.User clicked on icon which opened modal window, since it's opened the modal window it's not going to change the URL on the address bar.
3.Modal window(Modalwindow.html ) has the link of show product, since the product page is the active page(show product is the accordion which closed by user explicitly) on the browser.
on the click of link, appropriate accordion should be open on the product.html
I am communicating between modal window controller (controller2.js) and product page(controller1.js) through service (service.js), I am calling controller2
how to fix this issue without loading a complete page
Assuming the modal closes when a product is selected, it can return the value to the calling controller. Then it opens the specified accordion.
I fiddled around in your fiddle. You are mixing two ways of showing your categories in the fiddle: an accordion value, and two boolean values (categoryAccordion, productAccordion). I moved to using one way and it seems to work with the eventCallback. Also, you checked wrongly for your 'args' in the eventCallback. You're passing it back as an array, so get the value out of the array first.
Also, you checked wrongly for your 'args' in the eventCallback.`
if (args[0] == 'Product') {
$scope.productAccordion = true;
$scope.categoryAccordion = false;
} else {
$scope.productAccordion = false;
$scope.categoryAccordion = true;
}
See fiddle.
Should it not be working in your real code, it might have something to do with the following SO question.

UI Testing failure - No matches found for SearchField

My XCode recorded UI test fails on the second line:
XCUIElement *musicselectviewNavigationBar = app.navigationBars[#"MusicSelectView"];
[[musicselectviewNavigationBar.otherElements childrenMatchingType:XCUIElementTypeSearchField].element tap];
[[musicselectviewNavigationBar.searchFields containingType:XCUIElementTypeButton identifier:#"Clear text"].element typeText:#"Lean"];
Upon testing I get this error:
UI Testing failure - No matches found for "MusicSelectView" NavigationBar
Ideas?
Access the search bar directly instead of using the recorded path.
[app.searchFields[#"Clear text"] tap];
[app.searchFields[#"Clear text"] typeText:#"Lean"];
If there is only one search field present, you can access it with element.
[[app.searchFields element] tap];
[[app.searchFields element] typeText:#"Lean"];
check what you have at line 1 in app.navigationBars
set your navigation bar accessibilityIdentifier to #"MusicSelectView"
access your navigation bar with matchingIdentifier:#"MusicSelectView" method call