Unable to automate a click using Python and Selenium - selenium

I'm trying to automate adding requirements to a TestLink database. I'm running into an issue trying to click on this anchor/span.
<a hidefocus="on" class="x-tree-node-anchor" href="javascript:REQ_SPEC_MGMT(17473)" tabindex="1">
<span unselectable="on" id="extdd-6">0:Project-0 (0)</span>
</a>
Here is the section of Python code with things I've tried:
anchor = browser.find_element_by_xpath('//a[contains(#href, "REQ_SPEC_MGMT")]')
span = anchor.find_element_by_xpath('.//span')
anchor.click() # Doesn't work
span.click() # Doesn't work
browser.execute_script("arguments[0].click();", anchor) # Doesn't work
browser.execute_script("arguments[0].click();", span) # Doesn't work
I don't get any errors, but I still don't get the page that appears when I manually click on the link. I verified that I'm finding the correct anchor/span by dumping out the properties so I know I have the correct elements. I've also tried long pauses just to make sure that the element is clickable before I try it. Any ideas on what I am doing wrong? Thanks!
Update - Here's a larger section of the HTML:
<div id="tree_div" style="overflow:auto; height:100%;border:1px solid #c3daf9;" class=" x-panel x-tree">
<div class="x-panel-bwrap" id="ext-gen12"><div class="x-panel-body x-panel-body-noheader" id="ext-gen13" style="overflow: auto;">
<ul class="x-tree-root-ct x-tree-arrows" id="ext-gen14">
<li class="x-tree-node"><div ext:tree-node-id="17472" class="x-tree-node-el x-unselectable x-tree-node-expanded" unselectable="on" id="extdd-1">
<span class="x-tree-node-indent"></span>
<img alt="" src="http://192.168.11.111/third_party/ext-js/images/default/s.gif" class="x-tree-ec-icon x-tree-elbow-end-minus">
<img alt="" src="http://192.168.11.111/third_party/ext-js/images/default/s.gif" class="x-tree-node-icon" unselectable="on" id="extdd-2">
<a hidefocus="on" class="x-tree-node-anchor" href="javascript:TPROJECT_REQ_SPEC_MGMT(17472)" tabindex="1">
<span unselectable="on" id="extdd-3">ProjTasks (0)</span>
</a>
</div>
<ul class="x-tree-node-ct" style="">
<li class="x-tree-node">
<div ext:tree-node-id="17473" class="x-tree-node-el x-unselectable folder x-tree-node-collapsed" unselectable="on" id="extdd-4">
<span class="x-tree-node-indent"><img alt="" src="http://192.168.11.111/third_party/ext-js/images/default/s.gif" class="x-tree-icon"></span>
<img alt="" src="http://192.168.11.111/third_party/ext-js/images/default/s.gif" class="x-tree-ec-icon x-tree-elbow-plus">
<img alt="" src="http://192.168.11.111/third_party/ext-js/images/default/s.gif" class="x-tree-node-icon" unselectable="on" id="extdd-5">
<a hidefocus="on" class="x-tree-node-anchor" href="javascript:REQ_SPEC_MGMT(17473)" tabindex="1">
<span unselectable="on" id="extdd-6">0:Project-0 (0)</span></a></div><ul class="x-tree-node-ct" style="display:none;">
</ul>
</li>

At first, try to use implicit selenium wait, to make sure that all your elements is download.
anchor = WebDriverWait(browser,30).until(EC.element_to_be_clickable((By.XPATH, '//a[contains(#href, "REQ_SPEC_MGMT")]')))
span = WebDriverWait(anchor,30).until(EC.element_to_be_clickable((By.XPATH, './/span')))
If it does'n work fine, try to execut js script in href:
browser.execute_script("javascript:REQ_SPEC_MGMT(17473)")
or
browser.execute_script(anchor.get_attribute("href"))

I figured out the issue. Some of the characters I was passing to execute_script were being dropped by my vnc software (Python Selenium script drops keys but only when used on a VNC).

To click() on the element with text as 0:Project-0 (0) element is an JavaScript enabled element so to click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.x-tree-node-anchor[href*='REQ_SPEC_MGMT']>span[id^='extdd-']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='x-tree-node-anchor' and contains(#href, 'REQ_SPEC_MGMT')]/span[starts-with(#id, 'extdd-') and text()='0:Project-0 (0)']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Related

Selenium webdriver can't find xpath

element code:
<div class="col col-2"><div class="v-input v-input--is-focused theme--light v-text-field v-text-field--is-booted v-select primary--text"><div class="v-input__control"><div role="button" aria-haspopup="listbox" aria-expanded="false" aria-owns="list-65" class="v-input__slot"><div class="v-select__slot"><label for="input-65" class="v-label v-label--active theme--light primary--text" style="left: 0px; right: auto; position: absolute;">Narystė</label><div class="v-select__selections"><input id="input-65" readonly="readonly" type="text" aria-readonly="false" autocomplete="off"></div><div class="v-input__append-inner"><div class="v-input__icon v-input__icon--append"><i aria-hidden="true" class="v-icon notranslate material-icons theme--light primary--text">arrow_drop_down</i></div></div><input type="hidden" value="[object Object]"></div><div class="v-menu"><!----></div></div><div class="v-text-field__details"><div class="v-messages theme--light primary--text"><div class="v-messages__wrapper"></div></div></div></div></div></div>
in screen you can see, that entered xpath is only one.
Here is code, where i have entered the same xpath, but getting error like this:
private static final By naryste = By.xpath("//*[contains(#class,'v-label v-label--active theme--light primary--text') and contains(text(),'Narystė')]");
#Step("Pasirenkame juridinio asmens organizaciją iš reikšmių sąrašo")
public createOrganization selectMembership() {
button.click(naryste);
return this;
}
Selenium webdriver can't find this xpath:
Find element :By.xpath: //*[contains(#class,'v-label v-label--active theme--light primary--text') and contains(text(),'Narystė')]
P.S. the same problem if i choose other elements
AND this (selenium can't find)
To locate the element you can use the following Locator Strategy:
Using xpath:
By.xpath("//label[starts-with(#for,'input') and contains(.,'Narystė')]");
PS: Do add some waits before invoking the click()

Web scraping Linkedin Job posts using Python, Selenium & Phantomjs

Some LinkedIn job posts contain a see more button that expands the whole job description:
https://www.linkedin.com/jobs/view/401243784/?refId=3024203031501300167509&trk=d_flagship3_search_srp_jobs
I tried to expand it using the element.click() but the source I get after expansion contains some placeholder divs instead of the original div. How, can I scrap those hidden texts.
This is what I get from driver.page_source
<div class="jobs-ghost-placeholder jobs-ghost-placeholder--medium jobs-ghost-placeholder--thin mb2"></div>
<div class="jobs-ghost-placeholder jobs-ghost-placeholder--x-small jobs-ghost-placeholder--thin mb2"></div>
<div class="jobs-ghost-placeholder jobs-ghost-placeholder--small jobs-ghost-placeholder--thin"></div>
Instead of the source I get from chrome inspect:
<div id="ember7189" class="jobs-description-details pt5 ember-view"> <h3 class="jobs-box__sub-title js-formatted-exp-title">Seniority Level</h3>
<p class="jobs-box__body js-formatted-exp-body">Associate</p>
<!---->
<h3 class="jobs-box__sub-title js-formatted-industries-title">Industry</h3>
<ul class="jobs-box__list jobs-description-details__list js-formatted-industries-list">
<li class="jobs-box__list-item jobs-description-details__list-item">Real Estate</li>
<li class="jobs-box__list-item jobs-description-details__list-item">Information Technology and Services</li>
</ul>
<h3 class="jobs-box__sub-title js-formatted-employment-status-title">Employment Type</h3>
<p class="jobs-box__body js-formatted-employment-status-body">Full-time</p>
<h3 class="jobs-box__sub-title js-formatted-job-functions-title">Job Functions</h3>
<ul class="jobs-box__list jobs-description-details__list js-formatted-job-functions-list">
<li class="jobs-box__list-item jobs-description-details__list-item">Information Technology</li>
<li class="jobs-box__list-item jobs-description-details__list-item">Project Management</li>
<li class="jobs-box__list-item jobs-description-details__list-item">Product Management</li>
</ul>
</div>
I also tried different values for the wait WebDriverWait(driver, 3) but in vain.
code:
employment_type = wait.until(EC.presence_of_element_located(
(By.CSS_SELECTOR, 'div.jobs-description__details>div.jobs-description-details>p.js-formatted-employment-status-body'))).text
raises timeout exception as it only finds those jobs-ghost-placeholder instead of the described css_selector

How would one locate following object [ul, li] in selenium webdriver?

Could you help me in figuring out how to proceed with locating "new" using selenium webdriver in Java.
Code Snippet:
<ul class="ms-crm-CommandBar-Menu">
<li tabindex="-1" class="ms-crm-CommandBarItem ms-crm-CommandBar-Menu ms-crm-CommandBar-Button" title="New Create a new Account record." id="account|NoRelationship|HomePageGrid|Mscrm.HomepageGrid.account.NewRecord" command="account|NoRelationship|HomePageGrid|Mscrm.NewRecordFromGrid" style="white-space: pre-line; display: inline-block;"><span tabindex="-1" class="ms-crm-CommandBar-Button ms-crm-Menu-Label" style="max-width:200px"><a tabindex="0" class="ms-crm-Menu-Label" onclick="return false"><img tabindex="-1" class="ms-crm-ImageStrip-New_16 ms-crm-commandbar-image16by16" src="/_imgs/imagestrips/transparent_spacer.gif" style="vertical-align:top" alt="New"> <span tabindex="-1" class="ms-crm-CommandBar-Menu" style="max-width:150px" command="account|NoRelationship|HomePageGrid|Mscrm.NewRecordFromGrid"> New </span> </a> </span> </li>
Disclaimer: I'm using C# but shouldn't make much difference.
The easiest would be using a CssSelector. It's a bit verbose in this case, but that doesn't matter:
driver.FindElement(By.CssSelector("span[command=\"account|NoRelationship|HomePageGrid|Mscrm.NewRecordFromGrid\"]"));
This will find the span element which contains nothing but the text you want. Assuming the command is unique on the page, otherwise you'll need to refine.
Alternatively you can use XPath to achieve the same:
driver.FindElement(By.XPath("//span[#command='account|NoRelationship|HomePageGrid|Mscrm.NewRecordFromGrid']");

How to Click on a Text in Selenium Webdriver 2.x

I am not able to click on the below HTML values through selenium webdriver click command through Java.
Here's my HTML...I have to click on PAAcctAcctRels, PAAcctActivityData, etc. as in the HTML.
I tried with LinkText (driver.findElement(By.linkText("PAAcctAcctRels")).click();) and xpath (driver.findElement(By.xpath(".//[#id='primaryNavLevel2Z6_G868H4S0K881F0AAEO37LG28N0']/div[1]/a")).click();)
<div id="primaryNavLevel2Z6_0G5A11K0KGF200AIUB98T20G52" class="dropdown_1columns">
<div class="col_1">
<a class="" href="?uri=nm:oid:Z6_0G5A11K0KGF200AIUB98T20G53">
<strong>
<span lang="en" dir="ltr">
PAAcctAcctRels
<span class="wpthemeAccess"> currently selected</span>
</span>
</strong>
</a>
</div>
<div class="col_1">
<a class="" href="?uri=nm:oid:Z6_0G5A11K0KGF200AIUB98T20GD4">
<span lang="en" dir="ltr">PAAcctActivityData</span>
</a>
</div>
<div class="col_1">
<a class="" href="?uri=nm:oid:Z6_0G5A11K0KGF200AIUB98T20GT1">
<span lang="en" dir="ltr">PAAcctAddrEmail</span>
</a>
</div>
Is there any other way to do this..please let me know.
1- For Clicking on text 'PAAcctActivityData', you can use the below code:
driver.findElement(By.xpath("//span[.='PAAcctActivityData']")).click();
2- For Clicking on text 'PAAcctAddrEmail', you can use the below code:
driver.findElement(By.xpath("//span[.='PAAcctAddrEmail']")).click();
NOTE:- The above xpaths will locate thespan elements with exact innerHTML/text as 'PAAcctActivityData' or 'PAAcctAddrEmail', respectively.
By.linkText("PAAcctAcctRels") won't work because that link has more text (ie ' currently selected'), and the problem with your xpath is that is starts with .//
The following should work (I have avoided using * for performance)
By.xpath("//div[#id='primaryNavLevel2Z6_G868H4S0K881F0AAEO37LG28N0']/div[1]/a")
Try using //[#id='primaryNavLevel2Z6_G868H4S0K881F0AAEO37LG28N0']/div[1]/a/span
as xpath. Remove the initial '.' and add '/span' at the end.

Selenium WebDrivers -Not able to select dynamic check box which has no name

<div id="checkboxfield-3844" class="x-field x-form-item x-field-default x-form-cb-checked x-form-dirty" style="width: 492px;">
<label id="checkboxfield-3844-labelEl" class="x-form-item-label x-form-item-label-left" style="margin-right:5px;width:200px;" for="ext-gen6460">Is Stitching Point:</label>
<div id="checkboxfield-3844-bodyEl" class="x-form-item-body x-form-cb-wrap" role="presentation" style="width: 287px;">
<input id="ext-gen6460" class="x-form-field x-form-checkbox" type="button" hidefocus="true" autocomplete="off" aria-checked="true" aria-invalid="false" role="checkbox" aria-describedby="checkboxfield-3844-errorEl" style="-moz-user-select: text;" data-errorqtip="">
</div>
<div id="checkboxfield-3844-errorEl" class="x-form-error-msg" style="display:none"></div>
<div class="x-clear" role="presentation"></div>
</div>
This is my Div , out of it iam not able to select the checkbox since there is no name .Need some help on this
Assuming there is only one 'label tag' with innerHTML/text as "Is Stiching Point:", the below xpath will point to the checkbox:-
driver.findElement(By.xpath("//label[.='Is Stitching Point:']/..//input[#role='checkbox']")).click();
NOTE: Above is a java code. In case you are using a different language binding, you can refer this to employ the same thing.
Edit
Below is the alternative way using JavascriptExecutor:
WebElement element = driver.findElement(By.xpath("//label[.='Is Stitching Point:']/..//input[#role='checkbox']"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);