.click function for selenium works inconsistently - chrome - selenium

I have this simple code to click the first paper link Organoid Modeling of the Tumor Immune Microenvironment. at this link.
title_wait = WebDriverWait(driver,5).until(
EC.presence_of_element_located((By.CLASS_NAME, "docsum-title")))
print('found title '+str(title))
element = WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.LINK_TEXT, str(title)))).click();
print('found link to click')
My code will sometimes work, but around 50% of the time it just skips right over the .click() and goes to the print below. Any help would be appreciated!

Actually, the problem is not with the wait time. U have made a small mistake in this line: element = WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.LINK_TEXT, str(title)))).click();
First of all, title is a variable of type selenium.webdriver.remote.webelement.WebElement. When u convert this into a str, u get this: <selenium.webdriver.remote.webelement.WebElement (session="21a8944e81b4dce8386fdf91067a2ddd", element="17660b77-61f4-4b2d-b5e8-f845ce97ad1e")>. So this is not the right way to get the text.
The right way is to use .text. Replace str(title) with title.text. Ur code should work. Here is the final code:
title = WebDriverWait(driver,5).until(
EC.presence_of_element_located((By.CLASS_NAME, "docsum-title")))
print('found title '+ title.text)
element = WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.LINK_TEXT , title.text))).click();
print('found link to click')
As Arundeep pointed out, u can increase the wait time to make ur code better. But this was the main problem in ur code.

Related

Can t find the xpath for Following button instagram for selenium

I try to get the xpath for the following button on instagram making an automate unfollowing soft. enter image description here
I found it just like this:
driver.find_element_by_xpath('//div[#class="qF0y9 Igw0E rBNOH YBx95 ybXk5 _4EzTm soMvl "]').click()
But i want to itterate over all ,,Following" Buttons , but like this is stuck at the first one!
This is my Code:
fBody = driver.find_element_by_xpath("//div[#class='isgrP']")
for i in range(1, 1500):
driver.find_element_by_xpath('//div[#class=" qF0y9 Igw0E rBNOH YBx95 ybXk5 _4EzTm soMvl "]').click()
#driver.find_element_by_xpath("//*[text()='Following']").click()
print("Am apasat follow")
sleep(5)
driver.find_element_by_xpath('//button[#class="aOOlW -Cab_ "]').click()
sleep(5)
driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', fBody)
print("Ma bag la somn 1 min")
sleep(2)
print("salut")
Selenium does Not "like" empty or white spaces in the attributes.
I suggest using a CSS selector and using *= in order to find text contains:
driver.find_element_by_CSS('//div[class*="qF0y9"][class*="Igw0E"]').click();
Avoid using white or empty spaces and, underscores (_) and hyphens (-) for the element's attributes.
I think the classes on the elements change as yours do not match with mine. Here is a more generic XPath that matches the "following" button.
//div//button[div[text()='Following']
When using this in a test I found it instantly failing unless I surrounded it with an explicit wait condition.
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div//button[div[text()='Following']"))).click();
Ill post my example when Instagram stops giving me connectivity issues.

How to get the clicked element in selenium when we don't know what are we clicking

I am clicking with the help of following lione oc code->
actions.moveToElement(objDriver.findElement(By.id("id_popcode")),coordinates.getX(),coordinates1.getY()-1).doubleClick().build().perform();
Basically i double click at a position(x,y) in our application. Individually we cannot click that particular element bcoz it has to be clicked at particular (x,y) itself. So i want to get the properties of that clicked element(which i click using actions command which i mentioned above) liked id, classname. Can some one help me with this...kinda stuck here..........
edit:
try execute.elementFromPoint() with JavascriptExecutor to get element by coordinates
JavascriptExecutor js = (JavascriptExecutor)driver;
WebElement theElement = (WebElement)js.executeScript("return document.elementFromPoint(arguments[0], arguments[1])", coordinates.getX(), coordinates1.getY()-1);
System.out.println(theElement.getAttribute("tagName"));
System.out.println(theElement.getAttribute("class"));
old:
you are using negative value in getY()-1 which mean coordinates above the element, it maybe the parent or preceding-sibling of element try to select it using xpath
WebElement popcodeBefore = objDriver.findElement(By.xpath("//*[#id='id_popcode']/parent::*"));
// Or
// WebElement popcodeBefore = objDriver.findElement(By.xpath("//*[#id='id_popcode']/preceding-sibling::*"));
System.out.println(popcodeBefore.getAttribute("class"));
actions.moveToElement(popcodeBefore).doubleClick().build().perform();
If you have any specific text at that particular coordinates make use of it. I too had the same issue like this where I need to double click on a cell which had text 0.00%. I have done hovering action first using the text and then performed the double-click
Ignore the syntax issues since I am working on the protractor these days
browser.driver.actions().mouseMove(driver.findElement(by.xpath("//*[text()='00')]").build().perform();
and then perform the click
Still, you have issues, check if you have any attribute like ng-click which can be helpful to get the coordinates for that particular location. please always share the HTML code so that It may help us to check more deeply

Octave printf does not output when followed by ginput

I am trying to make a prompt for the user to select from the figure (plot).
When I run it with the code below, the prompt doesnt display until i click on the figure, after which the prompt displays and the code continues. In fact, no call to printf (or disp) that is called before the ginput call displays until i select the figure.
printf("Select part\n"); % (disp also doesnt work properly)
[xinput,yinput] = ginput(1);
The purpose of the prompt is to alert the user to move to the figure, so naturally it needs to display before selecting the figure.
I can add an extra redundant input call between the two which forces the printf to display in the console. eg input("Press Enter"). but this is an inconvenient solution.
Strangely, if you run just the code above it does work normally. But when running in the remainder of the program it displays the issue. So it may be a difficult one to debug. Also, running it one line at a time in the full code using the debugger works properly, displaying the prompt before selecting the figure.
Just to add to the confusion. When running this part of the program in a loop, the first instance doesnt display the prompt correctly, but every other instance it works.
Thanks
EDIT:
The following code reliably fails (for me) in the same way my full program fails; (edited again to simplify)
figure(1);
input_test = input("press 1: ");
switch input_test
case 1
while true
clc;
printf("Left click to get coords or right click to finish\n");
[xinput,yinput,mouse_button] = ginput(1)
if mouse_button == 3
break
endif
endwhile
endswitch
It appears it has something to do with the line;
input_test = input("press 1: ");
If I replace this with
input_test = 1;
it works properly.
I dont know what is the reason for this, and I cannot remove the input request from this location.
Thanks Roger, you were correct, I did find a solution.
Using
fflush(stdout)
before the 'ginput' call solves the problem.
Found this in the 'input' help;
"Because there may be output waiting to be displayed by the pager,
it is a good idea to always call 'fflush (stdout)' before calling
'input'. This will ensure that all pending output is written to
the screen before your prompt."

How to display multiplication result in a textbox using Selenium WebDriver?

I am a manual tester and want to switch to automation testing. I've learnt Selenium WebDriver recently. While practicing, I came across a webpage where I was asked to automate the following thing in a web form:
In the form, they have provided two double numbers and have asked us to display the multiplication of thosenumbers in the textbox. Please guide me how can I display the result in the textbox using selenium webdriver.
Screenshot of the xpath
Add screen shot using below steps-
Click "Edit" to your question.
On upper side you can find Image icon or press "Ctrl + G".
Drag & drop the image or attach by providing the path.
[Note: I have added this as an answer because I don't have 50 points yet to add a comment. Forgive me for that.. :)]
If the two double numbers are in separate input field (any other place except input filed) get the data as follows:
double number01 = Double.parseDouble(driver.findElement(By.cssSelector("your selector for element")).getText());
double number02 = Double.parseDouble(driver.findElement(By.cssSelector("your selector for element2")).getText());
Then perform multiplication and use sendkeys to input into the output field:
driver.findElement(By.cssSelector("your selector for element2")).sendKeys((number01 * number02) + "");
Note:
Above answer is based on assumption according to your question.
Edit:
String terms = driver.findElement(By.cssSelector(("#form > form > label:nth-child(89)"))).getText().replaceAll("=", "");//get the expression for multiplication and remove the '=' sign at the end
String [] temp_xy = terms.split("X"); // split the string into multiplicand and multiplier parts
double multiplicand = Double.parseDouble(temp_xy[0].trim());
double multiplier = Double.parseDouble(temp_xy[1].trim());
double product = multiplicand * multiplier;
Now, you put your product in the desired filed. Similarly, you can find the quotient.

Selenium cannot find the element under iframe

I'm trying to find the element under a iframe, and I've switch to the frame, but I still can not find the element
enter image description here
my HTML is in the link: http://pastebin.com/AShYrdxQ
Hi first of all i found only one iframe on the page with id = msgframe and also please note that as per your source code that i frame is commented so not playing any role hence please do not use switch to driver simply use
List<WebElement> commonElements = driver.findElements(By.className("Apps_Title"));
for(int i =0;i<commonElements.size();i++){
System.out.println(commonElements.get(i).getText());
}
and it will work thanks hope this will help you.
Try:
driver.switchTo().frame("needle-frame-id-or-name");
sometimes it's help me too:
driver.switchTo().defaultContent();
driver.switchTo().frame("needle-frame-id-or-name");