-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Complex XPATH is not found by Python Selenium #8190
Comments
👋 Hi there! Thank you for creating this issue. I am the Selenium Assistant Bot 🤖, I triage issues in this repository. If I can't do it, I label it to help maintainers identify issues that need triaging. I am an Open Source project 🙌, post bugs or ideas here! |
❗️ It seems this issue is not using any of the supported templates 💡 Supported issue types are (they start with):
Issue templates help this project to stay in shape, please use them and fill them out completely. By doing that you are helping the project because the community and maintainers can provide prompt feedback, and potentially solve the issue. If you are asking a question, a better way to address this is:
If you think this is incorrect, please feel free to open a new issue. Thank you for your contributions. |
This is likely due to phantomJs can’t do that. Since phantomJs is no longer maintained try Chrome or Firefox headless.
…On Apr 10, 2020, 12:23 PM +0100, selenium-assistant[bot] ***@***.***>, wrote:
Closed #8190.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Problem summary:
Selenium does not find elements with "too complex" XPATH
This is the HTML document to reproduce the problem:
<script>`
function getElementByXpath(path) {
result1 = document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
try {
evaluator = new XPathEvaluator();
xpath_expr = evaluator.createExpression(path, evaluator.createNSResolver(document)); //fails in MS Edge
result2 = xpath_expr.evaluate(document, XPathResult.FIRST_ORDERED_NODE_TYPE).singleNodeValue;
} catch(e) {
result2 = "";
}
}
function test_xpath() {
path1 = '//div[contains(@Class,"subject company_info")]/div[1]/text()[2]';
path2 = '//*[@id="tabs-info"]/div[4]/div[1]/text()[2]';
e1 = getElementByXpath(path1);
r1 = document.getElementById("i1")
e2 = getElementByXpath(path2);
r2 = document.getElementById("i2")
if (!e1) {
r1.textContent = "... Not found";
} else {
r1.textContent = e1.textContent;
}
if (!e2) {
r2.textContent = "... Not found";
} else {
r2.textContent = e2.textContent;
}
}
</script>My aim is to locate element with text "Private Limited Company".
According to both Chrome and Firefox Dev tools, its xpath is the following:
//*[@id="tabs-info"]/div[4]/div[1]/text()[1]
Javascript does not find this xpath in runtime, however the very similar xpath is found:
//*[@id="tabs-info"]/div[4]/div[1]/text()[2]
I opened tickets against the respective browsers about inconsistency between Dev Tools and Javascript engine.
However Selenium does not find any of them (NoSuchElementException is raised).
The element can be also located by xpath based on a class selector:
//div[contains(@class,"subject company_info")]/div[1]/text()[2]
Javascript finds it. Selenium does not find it, too.
This is my Python code to invoke Selenium:
`
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.set_window_size(1120, 550)
driver.get(url="file:///home/user/test.html")
path1 = '//*[@id="tabs-info"]/div[4]/div[1]/text()[2]'
path2 = '//div[contains(@Class,"subject company_info")]/div[1]/text()[2]'
e1 = driver.find_element(By.XPATH, path1)
print("Path #1: " + e1.text)
e2 = driver.find_element(By.XPATH, path2)
print("Path #2: " + e2.text)
driver.quit()
`
Tested with Firefox and Chrome on Linux. Also Selenium IDE playback fails with such kinds of xpath in both Firefox and Chrome on Windows 10.
The text was updated successfully, but these errors were encountered: