How to assert that specific element is located on the LEFT side of page? - location

enter image description hereAs part of my Selenium (Java) testing i need to make sure that Webelement is located on the left side of the page. In this situation how would you proceed and what action would you take?
Reference is attached

You can fetch the position of the WebElement in x and y coordinates.
Find a comfortable range for the value of x.
You can find a readthrough of this idea on this link
The Code may look like following -
WebElement eExportGridButton = driver.findElement(By.xpath("//*[contains(text(),'Export Grid')]"));
Actions actions = new Actions(driver);
int getX = eExportGridButton.getLocation().getX();
System.out.println("X coordinate: " + getX);

Related

Without a click event, determine element id or simulate a click event on a material ui button base and populate it with a character

I am writing a modified TicTacToe game where the user has to enter a correct response before their selected square receives an "O" or "X". I have this working fine with two live players on the same computer.
When I add the ability for one player to play against an automated response I am having trouble placing the "X". I can calculate the best square(row and column) to place the, "X", but am having trouble displaying it on the board. Below is the code that displays the buttons. The board layout can be a square of any number between 3 and 10.
I track the coordinates (data-coord) of each square but do not know how to use the row and column to place a square in the button group. I do not have a click event for the automated user but if there was a way to simulate the click event at the selected location then I could place the "X" similar to how the live user's selection is updated. Can you please provide any suggestions for doing this?
const boardgui = board.map((row, rowId) => {
const columns = row.map((column, columnId) => (
<Grid key={columnId} item>
<ButtonBase >
<Paper
onClick={(e) => {
clickSquareHandler(e);
}}
elevation={4}
data-coord={rowId + ':' + columnId}
className={classes.Paper}>
<Icon
className={classes.Icon}
style={{fontSize: 78}}>
</Icon>
</Paper>
</ButtonBase>
</Grid>
));
return (
<Grid
key={rowId}
className={classes.Grid}
container
spacing={2}>
{columns}
</Grid>)
})
My son was able to resolve this for me. Two steps.
Added the following line right below the data-coord in the jsx.
id={"Square" + rowId.toString() + columnId.toString()}
Added the following code to update the square. Note that the location of the selected square to be updated was determined with another algorithm. One of my problems was that I thought I needed to simulate a click but turned out not to be the case. I just needed to get the element by id, get the child property and then assign an "X" (ie 'close') to the innerHTML.
let x = selectSquare.x;
let y = selectSquare.y;
let squareId = "Square" + x.toString() + y.toString();
console.log ("Board - automaticMover - squareId = ", squareId)
let squareElement = document.getElementById(squareId);
console.log ("Board - automaticMover - doc = ", squareElement)
let target = squareElement;
console.log ("Board - automaticMover - target = ", target)
let parent = squareElement.parentElement;
console.log ("Board - automaticMover - parent = ", parent)
let child = squareElement.children[0];
console.log ("Board - automaticMover - child = ", child)
board[x][y] = 'X'
child.innerHTML = 'close';

Looking a way to automatically change one element on a website to another one. Hyperlink to a button exactly

So i have hyperlink at a webpage that is:
<b>456</b>
And i want it to be a button instead of hyperlink.
Like:
<input type="button" class="butt1" name="but" value="456" onclick="123.com'">
I tried to change it inside chrome dev tools and it works, well obviously since i jsut give it href adress manually. Sadly i have no expirience with tampermonkey or greasemonkey at all and very limited javascript knowledge. Wonder if it possible and would appretiate any help.
Here is a sample code based on your example...
First, you need to get the link
If it is only one link, find it based on a selector:
const a = document.querySelector('a.cs');
Create the input
const input = document.createElement('input');
input.type = 'button';
input.className = 'butt1';
input.name = 'but';
input.value = '456';
input.onclick = 'window.location.href=' + a.href;
Note: A better way is to use input.addEventListener('click', function) instead of onclick.
Replace the link with the button
a.parentNode.replaceChild(input, a);
If there are more than one links, then you need to get them and loop through them e.g.
const a = document.querySelectorAll('a.cs');

Selenium webdriver- Click on moving espot images - Amazon.in

I am trying to learn selenium by automating amazon.in
I would like to click on a moving image in an e-spot. It seems there is no class or id. Then how can i proceed?
WebDriver driver= new FirefoxDriver();
#Test
public void test() {
driver.get("http://amazon.in");
driver.manage().window().maximize();
WebElement menu = driver.findElement(By.xpath("//li[#id='nav_cat_2']"));
Actions builder = new Actions(driver);
builder.moveToElement(menu).build().perform();
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[#class='nav_a nav_item' and .=\"All Books\"]")));
WebElement menuOption = driver.findElement(By.xpath("//a[#class='nav_a nav_item' and .=\"All Books\"]"));
menuOption.click();
I have reached on the page. But dont know how to proceed after that.
URL
http://www.amazon.in/Books/b/ref=nav_shopall_books_all/280-9259056-7717210?_encoding=UTF8&node=976389031
As I can see, the images are getting scrolled, so just wait for the concerned image first and then click on it. I have added a code based on that:
wait = new WebDriverWait(driver,60);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[#class='acsux-hero-wrap']//li[2]//img")));
WebElement ele = driver.findElement(By.xpath("//div[#class='acsux-hero-wrap']//li[2]//img"));
ele.click();
In the above code, the driver waits till the visibility of the 2nd image is located under 60 seconds. Then, it clicks on that element.
Similarly you can just replace the number li[2] with "li[1]" for first element, "li[4]" for fourth element, and so on.
Use other attributes such as width or alt
//img[contains(#alt,'Children')]
I was able to click on the highlighted image shown in your screenshot although its a different image with different keywords i reckon you'll need some bonded attribute for that image there to be able to click it regardless of its content or so and that'll be something like know the exact div the img tag this and so on.
hope this helps

Code is not executing after window.load (Javascript)

(sorry for my bad english)
I have a big problem with which I've been beating me for several days but to which I do not find any solution, even while going to excavate very far in subjects on known forums (and less known).
I develop a small application in Javascript which must recover an array of links. I open these links one by one in the same page, and I click on a button (which posts a name), then I check after a small lapse of time that the name is well posted and corresponds to that present on the page (in a fixed div). At this time, I turn over on the basic page, then I start the script again with the second link contained in the array.
The problem is that the code is not carried out anymore after the window.load() function.
I test the code on Google Chrome (in the Javascript console) and it turns over me an error: “Uncaught ReferenceError: init is not defined … onload ".
I hope that you will be able to help me to find how to once carry out the code with the launching of the page it is launched since each bond opens a page in the relationship page.
Before, I had tested in a popup with the function window.open () (without “_parent”) and I wanted to close it thanks to window.close () function, but the code did not include/understand where to act since the remainder of the code was to now deal with the popup and not of the page on which the code was carried out at the beginning.
Here's the code :
//Here i get the links in an array
function recupHref(){
var lesHref = new Array();
var lesLiens = document.getElementsByTagName("a");
for(var i = 0; i < lesLiens.length; i ++)
if(lesLiens[i].parentNode.getAttribute("class") == "pubrhead-text-right")
lesHref.push(lesLiens[i].getAttribute("href"));
return lesHref;
}
var resultat = recupHref(); //I store them in a variable
//The main function which open the links one by one
//The while loop allows us to know if were subscribed or not
var o = function openLinks(){
for(var leIndex = 0; leIndex < resultat.length; leIndex ++){
window.open(resultat[leIndex], "_parent");
//Do i use window.load instead of DOMContentLoaded ?
addEventListener("DOMContentLoaded", function() {
document.getElementById("enbut").click();
var pseudo = document.getElementById("nameho").innerHTML;
var pseudok = document.getElementsByClassName("pname")[0].textContent;
});
while (pseudo === pseudok) {
!(window.open("http://page-with-links.html", "_parent"));
};
}
}
I thank you in advance, and I hope that you will include/understand my problem.
Here is a little draw to explain better than words :
In other words, just what i need is that : store links (done) --> Open the link --> Click on the button (done) --> Check if the 2 names are the same --> Came back to the first page/close popup/ (or go directly to the seconde link in the array) --> do this for the 2nd link, etc, etc.
Good day/evening.

Problems with GUI in Matlab

I have such code:
a=5;
b=a;
c=10;
u = (0:0.05*pi:2*pi)'; %'
v = [0:0.05*pi:2*pi];
X = a*sin(u)*cos(v);
Y = a*sin(u)*sin(v);
Z = c*cos(u)*ones(size(v));
Z(Z>0)=0; % cut upper
V1=4/3*pi*a*b*c;
d=1/2;
e=2^d;
a2=a/e;
b2=a/e;
c2=c;
V2=4/3*pi*a2*b2*c2;
X2 = a2*sin(u)*cos(v);%-2.5;
Y2 = b2*sin(u)*sin(v);
Z2 = c2*cos(u)*ones(size(v));%+0.25;
Z2(Z2>0)=0; % cut
h=1/3;
for j = 1:20
k1=(sin(pi*j/20)+0.5)^h;
a=a*k1;
c=c*k1;
X = a*sin(u)*cos(v);
Y = a*sin(u)*sin(v);
Z = c*cos(u)*ones(size(v));
Z(Z>0)=0;
a2=a2*k1;
b2=a2*k1;
c2=c2*k1;
X2 = a2*sin(u)*cos(v)+5;%-2.5;
Y2 = b2*sin(u)*sin(v);
Z2 = c2*cos(u)*ones(size(v));%+0.25;
Z2(Z2>0)=0;
hS1=surf(X,Y,Z);
alpha(.11)
hold on
hS2=surf(X2,Y2,Z2);
hold off
axis([-20 20 -20 20 -20 20]);
F(j) = getframe;
end
movie(F,4)
I have to input parameters a,b,c from the keyboard. I've made GUI & tried to do it by using "Edit text" with a function below, but it's not working((.
I can't understand what's the problem with it.
function a_edit_Callback(hObject, eventdata, handles)
user_entry = str2double(get(hObject,'string'));...
a=user_entry;
The problem is that your callback function executing your code is not 'seeing' the parameters you defined in your edit text callbacks. You need to establish your variables in the subfunction, since they aren't global.
Using guide, set up a uicontrol button to click when you've entered your parameters into your uicontrol edit text boxes. Under the callback of your button, place your above code, with the following at the top:
a=str2double(get(handles.a_edit,'String'));
b=str2double(get(handles.b_edit,'String'));
c=str2double(get(handles.c_edit,'String'));
This will pull in the current strings of your edit text uicontrols. (Assuming you've assigned the tag format x_edit for each of the edit text boxes in guide.)
EDIT:
Open the figure you already created with the edit text boxes. Next, check to make sure each of your text boxes have the tag a_edit, b_edit, c_edit by using the property inspector. Then create a button using guide, and open the property inspector by double clicking on it. Find the 'tag' field, and name it run. Save your figure, and open the corresponding M-file.
Next, find the line with run_Callback(hObject, eventdata, handles). Place the following under it:
a=str2double(get(handles.a_edit,'String'));
b=str2double(get(handles.b_edit,'String'));
c=str2double(get(handles.c_edit,'String'));
%# Add the rest of your code from above verbatim, minus the first three lines
This should be the ONLY code you add to the auto-generated M-file - don't mess with anything else until you get this much working. If you don't want the animation popping up randomly in your figure window, you can add a set of axes using guide as well.
From the looks of the code, it appears to be a 'script' and not a 'function'.
Did you just want a 'dialog (built-in GUI dialog)'? If so, you can add the following at the beginning of your script:
prompt = {'Enter the parameter value "a":','Enter the parameter value
"b":','Enter the parameter value "c":'};
dlg_title = 'Input the Parameter Values';
num_lines = 1;
def = {'5','5','10'};
answer = inputdlg(prompt,dlg_title,num_lines,def);
a=answer{1};a=str2double(a);
b=answer{2};b=str2double(b);
c=answer{3};c=str2double(c);
% Y.T.

Resources